[Javascript] Smart DFS O(n)
Intuition\n Obviously, the answer for any descendant of the node with num=1, is  1\n However, the answer for any other node, that is not an ancestor of 1, is al
**Intuition**\n* **Obviously, the answer for any descendant of the node with num=1, is  1**\n* **However, the answer for any other node, that is not an ancestor of 1, is also 1, as the numbers are distinct, therefore 1 cannot exist in any other subtree other than the ones of its ancestors.**\n\n**Implementation**\n\nThe latter bullet means that we only care about the chain starting from the root, and leading to the node with num[node]=1. If we process this chain in reverse (starting from the 1), we can keep track of the numbers used so far and just increment a counter as long as it\'s used.\n\n\n```\nvar smallestMissingValueSubtree = function(parents, nums) {\n    let n=parents.length,next=[...Array(n)].map(d=>[]),used={}\n    for(let i=1;i<n;i++)\n        next[parents[i]].push(i)\n    let dfs=(node)=>{\n        if(used[nums[node]])\n\t\t\treturn\n\t\tused[nums[node]]=true\n\t\tfor(let child of next[node])\n\t\t\tdfs(child)\n    }\n    let cur=nums.indexOf(1),leftAt=1,res=[...Array(n)].map(d=>1)\n    while(cur!==-1){\n        dfs(cur)\n        while(used[leftAt])\n            leftAt++\n        res[cur]=leftAt\n        cur=parents[cur]\n    }\n    return res\n};\n```
['Depth-First Search', 'JavaScript']