[Python3] post-order dfs
\n\nclass Solution:\n    def countHighestScoreNodes(self, parents: List[int]) -> int:\n        tree = [[] for _ in parents]\n        for i, x in enumerate(paren
\n```\nclass Solution:\n    def countHighestScoreNodes(self, parents: List[int]) -> int:\n        tree = [[] for _ in parents]\n        for i, x in enumerate(parents): \n            if x >= 0: tree[x].append(i)\n                \n        freq = defaultdict(int)\n        \n        def fn(x): \n            """Return count of tree nodes."""\n            left = right = 0\n            if tree[x]: left = fn(tree[x][0])\n            if len(tree[x]) > 1: right = fn(tree[x][1])\n            score = (left or 1) * (right or 1) * (len(parents) - 1 - left - right or 1)\n            freq[score] += 1\n            return 1 + left + right\n        \n        fn(0)\n        return freq[max(freq)]\n```\n\nAlternatively \n```\nclass Solution:\n    def countHighestScoreNodes(self, parents: List[int]) -> int:\n        tree = [[] for _ in parents]\n        for i, x in enumerate(parents): \n            if x >= 0: tree[x].append(i)\n        \n        def fn(x): \n            """Return count of tree nodes."""\n            count = score = 1\n            for xx in tree[x]: \n                cc = fn(xx)\n                count += cc\n                score *= cc\n            score *= len(parents) - count or 1\n            freq[score] += 1\n            return count\n        \n        freq = defaultdict(int)\n        fn(0)\n        return freq[max(freq)]\n```
['Python3']