Fastest Solution using Java | O(N) | DFS
Intuition\n Describe your first thoughts on how to solve this problem. \nCheck which values cannot have 1 as answer\n\n# Approach\n Describe your approach to so
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nCheck which values cannot have 1 as answer\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nSimply look the path which does not have 1 in it has the answer as one, so need only to check the path with 1 in it and go to its parent.\n\n# Complexity\n- Time complexity: O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\n\nclass Solution {\n    public int[] smallestMissingValueSubtree(int[] parents, int[] nums) {\n        HashMap<Integer, List<Integer>> graph = new HashMap<>();\n        int node = 0, len = parents.length, node_val_one = -1;\n        for(int parent : parents){\n            if(parent != -1){\n                if(!graph.containsKey(parent)){\n                    graph.put(parent, new ArrayList<Integer>());\n                }\n                graph.get(parent).add(node);\n            }\n            if(nums[node] == 1){\n                node_val_one = node;\n            }\n            node++;\n        }\n        //System.out.println("graph is: "+graph);\n        int root = 0;\n        int res[] = new int[len], pos = 1;\n        int[] vis = new int[len+2];\n\n        Arrays.fill(res,1);\n        if(node_val_one == -1){\n            return res;\n        }\n    \n        while(node_val_one != -1){\n            //System.out.println("calling for node: "+node_val_one);\n            getRes(graph, vis, node_val_one, nums);\n            while(vis[pos] == 1){\n                pos++;\n            }\n            res[node_val_one] = pos;\n            node_val_one = parents[node_val_one];\n        }\n\n        return res;\n    }\n\n    public void getRes(HashMap<Integer, List<Integer>> graph, int vis[], int node, int nums[]){\n        \n        if(vis[nums[node]] == 1){\n            return;\n        }\n        vis[nums[node]] = 1;\n        if(graph.containsKey(node)){\n            for(int n : graph.get(node)){\n                getRes(graph, vis, n, nums);\n            }\n        }\n    }\n}\n```
['Depth-First Search', 'Java']