[Python] Intuitive Postorder Traversal Message Passing
Build Tree using dict(list)\n2. Post Order Traversal of tree (pass the numbers to its parent)\n3. Find solution for each node and save to ret\n\nSome time savin
1. Build Tree using dict(list)\n2. Post Order Traversal of tree (pass the numbers to its parent)\n3. Find solution for each node and save to ```ret```\n\nSome time saving methods:\n1. When set unioning the children, add smaller sets to the largest set, so iteration cost is reduced\n2. Start finding solutions from the max answer of the children. \n```\nclass Solution:\n    def smallestMissingValueSubtree(self, parents: List[int], nums: List[int]) -> List[int]:\n        \n        from collections import defaultdict\n        tree = defaultdict(list)\n        for n,p in enumerate(parents):\n            tree[p].append(n)\n        for i in range(len(nums)):\n            if i not in tree:\n                tree[i].append(-1)\n            \n        #print(tree)\n        ret = [0] * len(nums)\n        \n        def trav(node):\n            nonlocal ret \n            if node == -1:\n                return set(), 1\n            #nchild = 0\n            s = []\n            start = 0\n            \n            # iterate of children (postorder traversal)\n            for c in range(len(tree[node])):\n                s1, start1 = trav(tree[node][c])\n                #nchild += size1\n                s.append(s1)\n                start = max(start, start1)\n            \n            # this step is just union of children sets\n            # this implementation saves some time over set.union, because the largest subset is untouched\n            s = sorted(s,key = lambda x : len(x),reverse = True)\n            for small in s[1:]:\n                for e in small:\n                    s[0].add(e)\n            s[0].add(nums[node])\n            \n            # find answer\n            for i in range(start, 1000000):\n                if i not in s[0]:\n                    ret[node] = i\n                    start = i\n                    break\n                s[0].remove(i)\n            return s[0], start\n                    \n        trav(0)\n        return ret\n```
[]