[Java] Clean O(n) Solution
\nclass Solution {\n    public int[] smallestMissingValueSubtree(int[] parents, int[] nums) {\n        int n = parents.length;\n        int[] res = new int[n];\
```\nclass Solution {\n    public int[] smallestMissingValueSubtree(int[] parents, int[] nums) {\n        int n = parents.length;\n        int[] res = new int[n];\n        for (int i = 0; i < n; i++) {\n            res[i] = 1;\n        }\n        \n        int oneIndex = -1;\n        for (int i = 0; i < n; i++) {\n            if (nums[i] == 1) {\n                oneIndex = i;\n                break;\n            }\n        }\n        \n        // 1 not found\n        if (oneIndex == -1) {\n            return res;\n        }\n        \n        Map<Integer, Set<Integer>> graph = new HashMap<>();\n        for (int i = 1; i < n; i++) {\n            Set<Integer> children = graph.getOrDefault(parents[i], new HashSet<Integer>());\n            children.add(i);\n            graph.put(parents[i], children);\n        }\n        \n        Set<Integer> visited = new HashSet<Integer>();\n        \n        int parentIter = oneIndex;\n        int miss = 1;\n        while (parentIter >= 0) {\n            dfs(parentIter, graph, visited, nums);\n            while (visited.contains(miss)) {\n                miss++;\n            }\n            res[parentIter] = miss;\n            parentIter = parents[parentIter];\n        }\n        return res;\n    }\n    \n    public void dfs(int ind, Map<Integer, Set<Integer>> graph, Set<Integer> visited, int []nums) {\n        if (!visited.contains(nums[ind])) {\n            Set <Integer> children = graph.getOrDefault(ind, new HashSet<Integer>());\n        \n            for (int p : children) {\n                dfs(p, graph, visited, nums);\n            }\n            visited.add(nums[ind]);\n        }\n    }\n}\n```
['Java']