javascript dfs 876ms
\nlet g, a, res;\nconst smallestMissingValueSubtree = (parents, nums) => {\n    let n = parents.length;\n    a = nums;\n    g = initializeGraph(n);\n    for (le
```\nlet g, a, res;\nconst smallestMissingValueSubtree = (parents, nums) => {\n    let n = parents.length;\n    a = nums;\n    g = initializeGraph(n);\n    for (let i = 1; i < n; i++) g[parents[i]].push(i);\n    res = Array(n).fill(0);\n    dfs(0);\n    return res;\n};\n\nconst dfs = (x) => {\n    let set = new Set(), miss = 1;\n    for (const e of g[x]) {\n        let [nset, nmiss] = dfs(e); // accumulation\n        miss = Math.max(miss, nmiss);\n        set = unionSet(set, nset);\n    }\n    set.add(a[x])\n    while (set.has(miss)) miss++;\n    res[x] = miss;\n    return [set, miss];\n};\n\nconst unionSet = (s1, s2) => {\n    if (s1.size > s2.size) {\n        for (const e of s2) s1.add(e);\n        return s1;\n    } else {\n        for (const e of s1) s2.add(e);\n        return s2;\n    }\n};\n\nconst initializeGraph = (n) => { let G = []; for (let i = 0; i < n; i++) { G.push([]); } return G; };\n```
['Depth-First Search', 'JavaScript']