Java DFS based Solution
\n/*\n* All the gene values are distinct. There would be only one node having a gene value of 1.\n* Except for the nodes that are in path from the root to the n
```\n/*\n* All the gene values are distinct. There would be only one node having a gene value of 1.\n* Except for the nodes that are in path from the root to the node with gene value 1, all\n* the other nodes would have 1 as the first missing value.\n* Build the parent to child relationship. Start with the node having the gene value of 1. Do\n* a DFS traversal and keep on adding the gene value that are observed for the subtree at the\n* node. Iterate through the numbers 1..n, the first number which is not present in the \n* observed genes is the answer for this node.\n* Traverse up the hierarchy using the parents array to reach till the root node. While\n* performing the DFS for each node, the observed gene list would help in preventing the\n* repeated traversal of a path.\n*/\nclass Solution {\n    public int[] smallestMissingValueSubtree(int[] parents, int[] nums) {\n        int[] smallestValue = new int[parents.length];\n        for (int i = 0; i < nums.length; i++) {\n            smallestValue[i] = 1;\n        }\n        int oneIndex = -1;\n        for (int i = 0; i < parents.length; i++) {\n            if (nums[i] == 1) {\n                oneIndex = i;\n                break;\n            }\n        }\n        if (oneIndex == -1) {\n            return smallestValue;\n        }\n        Map<Integer, Set<Integer>> childrenMap = new HashMap<>();\n        for (int i = 1; i < parents.length; i++) {\n            Set<Integer> children = childrenMap.getOrDefault(parents[i], new HashSet<>());\n            children.add(i);\n            childrenMap.put(parents[i], children);\n        }\n        int missingValue = 1;\n        Set<Integer> visitedGene = new HashSet<>();\n        int currentNode = oneIndex;\n        while (currentNode != -1) {\n            dfs(nums, currentNode, childrenMap, visitedGene);\n            while (visitedGene.contains(missingValue)) {\n                missingValue++;\n            }\n            smallestValue[currentNode] = missingValue;\n            currentNode = parents[currentNode];\n        }\n        return smallestValue;\n    }\n    \n    private void dfs(int[] nums, int node, Map<Integer, Set<Integer>> childrenMap, Set<Integer> visitedGene) {\n        if (visitedGene.contains(nums[node])) {\n            return;\n        }\n        if (childrenMap.containsKey(node)) {\n            Set<Integer> children = childrenMap.get(node);\n            for (Integer child: children) {\n                dfs(nums, child, childrenMap, visitedGene);\n            }\n        }\n        visitedGene.add(nums[node]);\n    }\n}\n```
[]