Small to large merging and use child MEX
\nfrom collections import deque\n\nclass Solution:\n    def smallestMissingValueSubtree(self, parents: List[int], nums: List[int]) -> List[int]:\n        n = le
```\nfrom collections import deque\n\nclass Solution:\n    def smallestMissingValueSubtree(self, parents: List[int], nums: List[int]) -> List[int]:\n        n = len(parents)\n        q = deque([0])\n        graph = [[] for _ in range(n)]\n        for i in range(n):\n            if parents[i] != -1:\n                graph[parents[i]].append(i)\n        order = []\n        while q:\n            curr = q.pop()\n            order.append(curr)\n            for j in graph[curr]:\n                q.appendleft(j)\n        order.reverse()\n        vals = [set() for _ in range(n)]\n        res = [0] * n\n        for i in order:\n            vals[i].add(nums[i])\n            MEX = 1\n            for j in graph[i]:\n                MEX = max(MEX, res[j])\n                if len(vals[i]) < len(vals[j]):\n                    vals[i], vals[j] = vals[j], vals[i]\n                vals[i].update(vals[j])\n                vals[j] = set()\n            while MEX in vals[i]:\n                MEX += 1\n            res[i] = MEX\n \xA0 \xA0 \xA0 \xA0return res \n```
[]