Just passes all test cases
\nclass Solution {\npublic:\n    \n    vector<int> in;\n    vector<int> out;\n    vector<int> par;\n    int timer;\n    int one;//pos of value 1\n    vector<vec
```\nclass Solution {\npublic:\n    \n    vector<int> in;\n    vector<int> out;\n    vector<int> par;\n    int timer;\n    int one;//pos of value 1\n    vector<vector<int>> g;\n    \n    void dfs(int u, int p)\n    {\n        in[u] = ++timer;\n        par[u] = p;\n        for(auto &v : g[u])\n            if(v!=p)\n                dfs(v, u);\n        out[u] = ++timer;  \n    }\n    \n    bool isAnc(int u,int v)\n    {\n        return (in[u] <= in[v] && out[u] >= out[v]);\n    }\n    \n    set<int> s;\n    void helper(int u,vector<int> &nums){\n        for(auto &v: g[u] )\n        {\n            if(v != par[u] && !isAnc(v, one))helper(v,nums);\n        }\n\t    s.erase(nums[u]);\n    }\n    \n    vector<int> smallestMissingValueSubtree(vector<int>& parents, vector<int>& nums)    {\n        int n = nums.size();\n        in.resize(n+1,0);\n        out.resize(n+1,0);\n        timer = 0;\n        vector<int> mex(n);\n        par = parents;\n        int root_mex = -1;\n        g = vector<vector<int>>(n);\n        \n        for(int i = 1; i < n; ++i) {\n            g[i].push_back(parents[i]);\n            g[parents[i]].push_back(i);\n        }\n        one = -1;\n        for(int i = 0;i<n;i++){\n            if(nums[i] == 1){\n                one = i;\n                break;\n            }\n        }\n        \n        if(one == -1){\n            for(int i = 0;i<n;i++)mex[i]= 1;\n            return mex;\n        }\n        dfs(0,-1);\n        for(int i = 0; i < n; ++i) s.insert(nums[i]);\n        int cntr = 1;\n        for(auto el : s){\n            if(el != cntr){\n                root_mex = cntr;\n                break;\n            }\n            cntr++;\n        }\n        if(root_mex == -1)root_mex = cntr;\n        \n        for(int i = 0; i < n; ++i) {\n\t\tif(isAnc(one, i)) \n\t\t\ts.erase(nums[i]);\n\t\tif(!isAnc(i, one))\n\t\t\tmex[i] = 1;\n        }\n        mex[one] = *s.begin();\n        \n        int cur = par[one];\n        while(cur != -1) {\n            helper(cur,nums);\n            mex[cur] = *s.begin();\n            cur = par[cur];\n        }\n        mex[0] = root_mex;\n        return mex;\n    }\n};\n```
[]