dfs
Code\n\nclass Solution {\npublic:\n    vector<int> smallestMissingValueSubtree(vector<int>& par, vector<int>& nums) {\n        int n = size(par);\n        vecto
# Code\n```\nclass Solution {\npublic:\n    vector<int> smallestMissingValueSubtree(vector<int>& par, vector<int>& nums) {\n        int n = size(par);\n        vector<vector<int>> g(n);\n        for(int i=1; i<n; ++i){\n            int u = i, v = par[i];\n            g[v].push_back(u);\n        }\n\n        vector<int> path;\n        auto dfs = [&](auto&&dfs,int u,int p) -> bool {\n            path.push_back(u);\n            if(nums[u] == 1) return true;\n            for(int v: g[u]){\n                if(v != p && dfs(dfs,v,u)) return true;\n            }\n            path.pop_back();\n            return false;\n        };\n        dfs(dfs,0,-1);\n\n        vector<int> ans(n,1);\n        int miss = 1;\n        unordered_set<int> missing;\n        auto fn = [&](auto&&fn,int u,int p) -> void {\n            missing.insert(nums[u]);\n            for(int v: g[u]){\n                if(v != p && !missing.count(nums[v]))\n                    fn(fn,v,u);\n            }\n        };\n        for(auto it=path.rbegin(); it!=path.rend(); it++){\n            int u = *it;\n            fn(fn,u,par[u]);\n            while(missing.count(miss)) miss++;\n            ans[u] = miss;\n        }\n        return ans;\n    }\n};\n```
['Tree', 'Depth-First Search', 'C++']