<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 __init__(self):
        self.tree = None
        self.scoreToCount = {}

    def countHighestScoreNodes(self, parents):
        self.tree = self.buildTree(parents)
        self.countNode(0)
        # 计算最大分数出现的次数
        maxScore = 0
        for score in self.scoreToCount.keys():
            maxScore = max(maxScore, score)
        return self.scoreToCount[maxScore]

    # 计算二叉树中的节点个数
    def countNode(self, root):
        if root == -1:
            return 0
        # 二叉树中节点总数
        n = len(self.tree)
        leftCount = self.countNode(self.tree[root][0])
        rightCount = self.countNode(self.tree[root][1])

        # 后序位置，计算每个节点的「分数」
        otherCount = n - leftCount - rightCount - 1
        # 注意，这里要把 int 转化成 long，否则会产生溢出！！！
        score = max(leftCount, 1) * max(rightCount, 1) * max(otherCount, 1)
        # 给分数 score 计数
        if score in self.scoreToCount:
            self.scoreToCount[score] += 1
        else:
            self.scoreToCount[score] = 1

        return leftCount + rightCount + 1

    # 将 parents 数组转化成常规二叉树（邻接表形式）
    def buildTree(self, parents):
        n = len(parents)
        # 表节点 x 的左子节点为 tree[x][0]，节点 x 的右子节点为 tree[x][1]
        # 若 tree[x][0] 或 tree[x][1] 等于 -1 则代表空指针
        tree = [[-1, -1] for _ in range(n)]
        # 根据 parents 数组构建二叉树（跳过 parents[0] 根节点）
        for i in range(1, n):
            parent_i = parents[i]
            if tree[parent_i][0] == -1:
                tree[parent_i][0] = i
            else:
                tree[parent_i][1] = i
        return tree
        
43472