There is a binary tree rooted at 0 consisting of n nodes. The nodes are labeled from 0 to n - 1. You are given a 0-indexed integer array parents representing the tree, where parents[i] is the parent of node i. Since node 0 is the root, parents[0] == -1.
Each node has a score. To find the score of a node, consider if the node and the edges connected to it were removed. The tree would become one or more non-empty subtrees. The size of a subtree is the number of the nodes in it. The score of the node is the product of the sizes of all those subtrees.
Return the number of nodes that have the highest score.
Example 1:
Input: parents = [-1,2,0,2,0]
Output: 3
Explanation:
- 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.
Example 2:
Input: parents = [-1,2,0]
Output: 2
Explanation:
- 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.
Constraints:
n == parents.length
2 <= n <= 105
parents[0] == -1
0 <= parents[i] <= n - 1 for i != 0
parents represents a valid binary tree.
Solutions
Solution 1: DFS
Thinking
Removing a node scores the product of component sizes. With \(n \le 10^5\) we need all scores in one walk. Subtree sizes appear on the DFS unwind; the complement is \(n-\textit{cnt}\).
Build child lists and DFS from the root: multiply child sizes, then \(n-\textit{cnt}\) if nonzero. Track the maximum score and its multiplicity.
First, we construct a graph \(g\) based on the given parent array parents, where \(g[i]\) represents all child nodes of node \(i\). We define a variable \(\textit{ans}\) to represent the number of nodes with the highest score, and a variable \(mx\) to represent the highest score.
Then, we design a function dfs(i, fa) to calculate the score of node \(i\) and return the number of nodes in the subtree rooted at node \(i\).
The calculation process of the function dfs(i, fa) is as follows:
We first initialize a variable \(\textit{cnt} = 1\), representing the number of nodes in the subtree rooted at node \(i\); a variable \(\textit{score} = 1\), representing the initial score of node \(i\).
Next, we traverse all child nodes \(j\) of node \(i\). If \(j\) is not the parent node \(fa\) of node \(i\), then we recursively call dfs(j, i), and multiply the return value into \(\textit{score}\), and add the return value to \(\textit{cnt}\).
After traversing the child nodes, if \(n - \textit{cnt} > 0\), then we multiply \(n - \textit{cnt}\) into \(\textit{score}\).
Then, we check whether \(mx\) is less than \(\textit{score}\). If it is less, then we update \(mx\) to \(\textit{score}\), and update \(\textit{ans}\) to \(1\); if it is equal, then we update \(\textit{ans}\) to \(\textit{ans} + 1\).
Finally, we return \(\textit{cnt}\).
In the end, we call dfs(0, -1) and return \(\textit{ans}\).
The time complexity is \(O(n)\), and the space complexity is \(O(n)\), where \(n\) is the number of nodes.
Removing a node scores the product of the remaining component sizes. \(n\) can reach \(10^5\). On a chain the first recursive call always follows the only child, so the depth is \(n\) and exceeds the default recursion limit.
The product needs every child's subtree size, and the component above the node has size \(n\) minus that subtree.
An explicit stack walks the tree in postorder. On entry, push the exit marker and then the children. On exit, multiply the child subtree sizes, then multiply by \(n-\textit{cnt}\) when a component remains above, and track the maximum score and how many nodes reach it.
First, we construct a graph \(g\) based on the given parent array parents, where \(g[i]\) represents all child nodes of node \(i\). We define a variable \(\textit{ans}\) to represent the number of nodes with the highest score, and a variable \(mx\) to represent the highest score.
An explicit stack walks the tree from root \(0\) in postorder. Entering a node pushes that node's exit marker and then its children, so the children finish first. On exit, each child's subtree size is already stored. Initialize \(\textit{cnt} = 1\) and \(\textit{score} = 1\). Multiply each child's subtree size into \(\textit{score}\) and add it to \(\textit{cnt}\). If \(n - \textit{cnt} > 0\), also multiply \(n - \textit{cnt}\) into \(\textit{score}\). If \(\textit{score}\) is greater than \(mx\), set \(mx\) to \(\textit{score}\) and \(\textit{ans}\) to \(1\); if they are equal, add \(1\) to \(\textit{ans}\). Store \(\textit{cnt}\) as the current node's subtree size.
The time complexity is \(O(n)\), and the space complexity is \(O(n)\), where \(n\) is the number of nodes.