BFS, C++, easy understand, beats 92%
\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<vector<int>> gm(N);\n        vector<int> minv(N);\n        \n        // 0. parent to children graph\n        for (int i = 1; i < N; i++) {\n            int pid = parents[i];\n            gm[pid].push_back(i);\n        }\n        \n        // 1. save the min value for each node and the node\'s subtree\n        get_minv(gm, nums, minv, 0);\n        \n        // 2. for each nod, find the smallest genetic value that is missing from the subtree rooted at node \n        //    that is the samllest the value from its parent, and the node\'s siblings\' min_values\n        vector<int> ans(N, 200000);\n        queue<int> mq;\n        \n        mq.push(0);\n        while(mq.size()) {\n            int id = mq.front();\n            mq.pop();\n            \n            if (gm[id].size()) {\n                int a = 200000, b = 200000;\n                for (auto cid : gm[id]) {\n                    int val = minv[cid];\n                    if (val < a) {\n                        b = a;\n                        a = val;\n                    }\n                    else if (val < b) {\n                        b = val;\n                    }\n                }\n                for (auto cid : gm[id]) {\n                    ans[cid] = min(nums[id], ans[id]);\n                    if (minv[cid] != a) {\n                        ans[cid] = min(ans[cid], a);\n                    }\n                    else if (minv[cid] != b) {\n                        ans[cid] = min(ans[cid], b);\n                    }\n                    \n                    mq.push(cid);\n                }\n            }\n        }\n        \n        // 3. in the case of missing values from nums, search the first missing value starting from 1\n        int tbl[N+1];\n        memset(tbl, 0, sizeof(tbl));\n        for (int i = 0; i < N; i++) {\n            int v = nums[i];\n            if (v <= N)\n                tbl[v]=1;\n        }\n        \n        int val = 1;\n        while(val <= N) {\n            if (tbl[val] == 0) break;\n            val++;\n        }\n        \n        // 4. ans is the min between ans[i] and the first missing value from nums\n        for (int i = 0; i < N; i++) {\n            ans[i] = min(ans[i], val);\n        }\n        \n        return ans;\n    }\n    \n    // for each node, save the min value into table including current node\n    int get_minv(vector<vector<int>> &gm, vector<int>& nums, vector<int> &minv, int id) {\n        if (gm[id].size() == 0) {\n            return minv[id] = nums[id];\n        }\n        \n        int ret = nums[id];\n        for (auto cid : gm[id]) {\n            ret = min(ret, get_minv(gm, nums, minv, cid));\n        }\n        return minv[id] = ret;\n    }\n};\n```\n
[]