2003. Java solution | T.C O(N) | S.C O(N)
\nApproach - \nFor example - Input: parents = [-1,0,1,0,3,3], nums = [5,4,6,2,1,3]\nTree -     \n\nIn example above, we can notice that node 4 has genetic value
\n**Approach** - \nFor example - Input: parents = [-1,0,1,0,3,3], nums = [5,4,6,2,1,3]\n**Tree** -     ![image](https://assets.leetcode.com/users/images/b702e600-38b8-4d66-8157-17909c85d516_1672075320.7666874.png)\n\nIn example above, we can notice that node 4 has genetic value 1. And root to node 4 path is rootToLeaf [0, 3, 4] it ends at node 4 which has genetic value of 1.\nFor rest of the nodes result[i] will be 1\nAnd for all the nodes which are part of rootToLeaf path, we will remove their values and the values of all the nodes in their repective subtrees from given range [1, 10^5]. And will check for the minimum value which is present in the that range.\nNote - we can use a treeset to store all the values from [1 to 10^5] or [1 to max(nums[i])] and will remove the present values from this treeset and we can find the minimum value using treeset.first() method.\nWe have to iterate from right to left in rootToLeaf. For any at node u at index i in rootToLeaf, do DFS only for nodes which is unequal to node at index (i + 1) or previos node in order to prevent to iterate the tree again and again which can lead to T.C of O(N*N);\nT.C is O(N) and S.C is O(N)\n\n```\nclass Solution {\n    private List<Integer> rootToLeaf;\n    public int[] smallestMissingValueSubtree(int[] parents, int[] nums) {\n        int n = parents.length;\n        rootToLeaf = new ArrayList<>();\n        ArrayList<Integer>[] adj = new ArrayList[n];\n        boolean oneAbsent = true;\n        int[] result = new int[n];\n        int maxValue = 0;\n        for(int i = 0; i<n; ++i){\n            result[i] = 1;\n            adj[i] = new ArrayList<>();\n            if(nums[i] == 1) oneAbsent = false; \n            maxValue = (int)Math.max(maxValue, nums[i]);\n        }\n        // Base case if genetic value 1 doesnot exist in nums[]\n        if(oneAbsent) return result;\n        int u, prevNode = -1;\n        for(int v = 1; v<n; ++v){\n            u = parents[v];\n            // edge is u-->v\n            adj[u].add(v);\n        }\n        \n        traverseFamilyTree(adj, 0, new ArrayList<>(), nums);\n        \n        // System.out.println(rootToLeaf.toString());\n        TreeSet<Integer> bst = new TreeSet<>();\n        for(int i = 1; i<=maxValue+1; ++i){\n            bst.add(i);\n        }\n        \n        for(int i = rootToLeaf.size()-1; i >= 0; --i){\n            u = rootToLeaf.get(i);\n            // System.out.printf("prevNode:%d, u:%d-->",prevNode, u);\n            for(int v : adj[u]){\n                if(v != prevNode){\n                    deleteGeneticValuesInSubtree(adj, v, bst, nums);\n                }\n            }\n            // System.out.println();\n            bst.remove(nums[u]);\n            result[u] = bst.first();\n            prevNode = u;\n        }\n        return result;\n    }\n    \n    private void traverseFamilyTree(ArrayList<Integer>[] adj, int u, List<Integer> path, int[] nums){\n        path.add(u);\n        if(nums[u] == 1){\n            rootToLeaf = new ArrayList<>(path);\n        }\n        for(int v : adj[u]){\n            traverseFamilyTree(adj, v, path, nums);\n        }\n        path.remove(path.size()-1);\n        return;\n    }\n    \n    private void deleteGeneticValuesInSubtree(ArrayList<Integer>[] adj, int u, TreeSet<Integer> bst, int[] nums){\n        // System.out.printf("%d, ", u);\n        bst.remove(nums[u]);\n        for(int v : adj[u]){\n            deleteGeneticValuesInSubtree(adj, v, bst, nums);\n        }\n        return;\n    }\n}\n```
['Tree', 'Java']