Highly Readable Code - Graph DS
\nclass Solution:\n    def smallestMissingValueSubtree(self, parents: List[int], nums: List[int]) -> List[int]:\n        n = len(parents)\n        root = None\n
```\nclass Solution:\n    def smallestMissingValueSubtree(self, parents: List[int], nums: List[int]) -> List[int]:\n        n = len(parents)\n        root = None\n        node_with_num_one = None\n        # construct graph\n        graph = defaultdict(list)\n        for i in range(n):\n            if parents[i] == -1:\n                root = i\n            else:\n                graph[parents[i]].append(i)\n            if nums[i] == 1:\n                node_with_num_one = i\n        \n        res = [1] * n\n        if node_with_num_one is None:\n            return res\n        # seen[i] = 1 means node i has been visited\n        seen = [0] * 100002\n        curr = node_with_num_one\n        prev_missing = 1\n        while curr != -1:\n            # mark the values of the subtree rooted at curr\n            self._dfs(curr, graph, seen, nums)\n            # find the first unseen num\n            for missing in range(prev_missing, 100002):\n                if seen[missing] == 0:\n                    res[curr] = missing\n                    break\n            prev_missing = missing\n            curr = parents[curr]\n        \n        return res\n    \n    def _dfs(self, root, graph, seen, nums):\n        """Do DFS from root and mark the values of the subtree rooted at root in seen.\n        """\n        if seen[nums[root]] == 0:\n            seen[nums[root]] = 1\n            for child in graph[root]:\n                self._dfs(child, graph, seen, nums)\n```
['Graph']