(C++) 2003. Smallest Missing Genetic Value in Each Subtree
\n\nclass Solution {\npublic:\n    vector<int> smallestMissingValueSubtree(vector<int>& parents, vector<int>& nums) {\n        int n = parents.size(); \n       
\n```\nclass Solution {\npublic:\n    vector<int> smallestMissingValueSubtree(vector<int>& parents, vector<int>& nums) {\n        int n = parents.size(); \n        vector<int> ans(n, 1); \n        \n        auto it = find(nums.begin(), nums.end(), 1); \n        if (it != nums.end()) {\n            vector<vector<int>> tree(n); \n            for (int i = 0; i < n; ++i) \n                if (parents[i] != -1) tree[parents[i]].push_back(i); \n            \n            int miss = 1; \n            unordered_set<int> seen; \n            for (int k = it - nums.begin(); k != -1; k = parents[k]) {\n                stack<int> stk; stk.push(k);\n                seen.insert(nums[k]); \n                while (stk.size()) {\n                    int x = stk.top(); stk.pop(); \n                    for (auto& xx : tree[x]) \n                        if (!seen.count(nums[xx])) {\n                            seen.insert(nums[xx]); \n                            stk.push(xx); \n                        }\n                }\n                for (; seen.count(miss); ++miss); \n                ans[k] = miss; \n            }\n        }\n        return ans; \n    }\n};\n```
['C']