Imagine you're a tree surgeon tasked with analyzing a special binary tree structure! ๐ณ
You have a binary tree rooted at node 0 with n nodes labeled from 0 to n-1. The tree structure is given as a parents array where parents[i] represents the parent of node i. Since node 0 is the root, parents[0] = -1.
Here's the interesting part: each node has a score calculated by a unique method. To find a node's score, imagine removing that node and all edges connected to it. This splits the tree into one or more separate subtrees. The node's score is the product of the sizes of all resulting subtrees.
Your goal: Find how many nodes achieve the maximum possible score.
Example: If removing a node creates subtrees of sizes [2, 3, 1], the score would be 2 ร 3 ร 1 = 6.
Input & Output
Constraints
- n == parents.length
- 2 โค n โค 105
- parents[0] == -1
- 0 โค parents[i] โค n - 1 for i != 0
- parents represents a valid binary tree