<p>There is a <strong>binary</strong> tree rooted at <code>0</code> consisting of <code>n</code> nodes. The nodes are labeled from <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> integer array <code>parents</code> representing the tree, where <code>parents[i]</code> is the parent of node <code>i</code>. Since node <code>0</code> is the root, <code>parents[0] == -1</code>.</p>

<p>Each node has a <strong>score</strong>. To find the score of a node, consider if the node and the edges connected to it were <strong>removed</strong>. The tree would become one or more <strong>non-empty</strong> subtrees. The <strong>size</strong> of a subtree is the number of the nodes in it. The <strong>score</strong> of the node is the <strong>product of the sizes</strong> of all those subtrees.</p>

<p>Return <em>the <strong>number</strong> of nodes that have the <strong>highest score</strong></em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="example-1" src="https://assets.leetcode.com/uploads/2021/10/03/example-1.png" style="width: 604px; height: 266px;" />
<pre>
<strong>Input:</strong> parents = [-1,2,0,2,0]
<strong>Output:</strong> 3
<strong>Explanation:</strong>
- The score of node 0 is: 3 * 1 = 3
- The score of node 1 is: 4 = 4
- The score of node 2 is: 1 * 1 * 2 = 2
- The score of node 3 is: 4 = 4
- The score of node 4 is: 4 = 4
The highest score is 4, and three nodes (node 1, node 3, and node 4) have the highest score.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="example-2" src="https://assets.leetcode.com/uploads/2021/10/03/example-2.png" style="width: 95px; height: 143px;" />
<pre>
<strong>Input:</strong> parents = [-1,2,0]
<strong>Output:</strong> 2
<strong>Explanation:</strong>
- The score of node 0 is: 2 = 2
- The score of node 1 is: 2 = 2
- The score of node 2 is: 1 * 1 = 1
The highest score is 2, and two nodes (node 0 and node 1) have the highest score.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n == parents.length</code></li>
	<li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li>
	<li><code>parents[0] == -1</code></li>
	<li><code>0 &lt;= parents[i] &lt;= n - 1</code> for <code>i != 0</code></li>
	<li><code>parents</code> represents a valid binary tree.</li>
</ul>

class Solution:
    def countHighestScoreNodes(self, parents: List[int]) -> int:
        # 9/2/2024
        # This is solution from openai! orignal solution below exceed the time.
        n = len(parents)
        children = defaultdict(list)
        for i in range(1, n):
            children[parents[i]].append(i)

        sub_tree_size = [0] * n
        max_score = 0
        count = 0

        def dfs(children, n, node):
            size = 1
            score = 1
            for c in children[node]:
                child_size = dfs(children, n, c)
                size += child_size
                score *= child_size
            sub_tree_size[node] = size

            # update score form parent
            if node != 0:
                score *= (n - size)
            
            # update global variables
            nonlocal max_score, count
            if score > max_score:
                count = 1
                max_score = score
            elif score == max_score:
                count += 1

            return size
        
        dfs(children, n, 0)
        return count
            

        # This solution has time exceed! -> travel the tree once for all socres.
        # constrcut tree dict
        # for each node, get left node score, right node score, root score

        # def construct_tree(lst, dt):
        #     for i, p in enumerate(lst):
        #         if p != -1:
        #             dt[p].append(i)
        #     # for leaf
        #     for i in range(len(lst)):
        #         if i not in dt:
        #             dt[i] = []
        
        # def score(dt, root, cur):
        #     if root not in dt:
        #         return 0
        #     res = 1
        #     for c in dt[root]:
        #         if c != cur:
        #             res += score(dt, c, cur)
        #     return res

        # n = len(parents)
        # dt = defaultdict(list)
        # construct_tree(parents, dt)
        # print(dt)
        # res = 0
        # mx = 0
        # for i in range(n):
        #     tmp = 1
        #     # print("i ", i)
        #     # find the root:
        #     ps = 0
        #     if i != 0:
        #         root = i
        #         while parents[root] != -1:
        #             root = parents[root]
        #         # print("root ", root)
        #         ps = score(dt, root, i)
        #     # print("ps ", ps)
        #     if ps > 0:
        #         tmp *= ps
        #     for c in dt[i]:
        #         cs = score(dt, c, i)
        #         if cs > 0:
        #             tmp *= cs
        #             # print("cs ", cs)
        #     if tmp > mx:
        #         mx = tmp
        #         res = 1
        #     elif tmp == mx:
        #         res += 1
        #     # print("mx", mx)
        #     # print("res ", res)
        # return res
53147