Path-Limited Traversal
If you traverse all nodes, you have to track their ranges independently, which is too slow.\n\n> I actually managed to get a full tree traversal AC (but barely 
If you traverse all nodes, you have to track their ranges independently, which is too slow.\n\n> I actually managed to get a full tree traversal AC (but barely - 2,000 ms), by tracking and merging numeric ranges. I posted that solution as a comment below.\n\nHowever, if you only traverse nodes starting from a node with `1` value (and then upstream to the root), you can just use a single `visited` array. \n\nNote that the result is always `1` for any node outside the path leading to `1` .\n\n**C++**\n```cpp\nbool visited[100002] = {};\nvoid dfs(vector<vector<int>> &nodes, vector<int>& nums, int i) {\n    if (!visited[nums[i]]) {\n        visited[nums[i]] = true;\n        for (int j : nodes[i])\n            dfs(nodes, nums, j);\n    }\n}\nvector<int> smallestMissingValueSubtree(vector<int>& parents, vector<int>& nums) {\n    vector<vector<int>> nodes(nums.size());\n    vector<int> res(nums.size(), 1);\n    for (int i = 1; i < nums.size(); ++i)\n        nodes[parents[i]].push_back(i);\n    int pos = find(begin(nums), end(nums), 1) - begin(nums), min = 1;\n    for (; pos < nums.size() && pos != -1; pos = parents[pos]) {\n        dfs(nodes, nums, pos);\n        while (visited[min])\n            ++min;\n        res[pos] = min;            \n    }\n    return res;\n}\n```
['C']