All Posts
Leetcode 2049: Count Nodes With the Highest Score
You are given a binary tree with n nodes, where each node is labeled from 0 to n-1. The tree is represented by a 0-indexed array parents
, where parents[i]
indicates the parent of node i
. The root node has no parent, so parents[0] == -1
.
Each node has a score, calculated as follows:
- If the node and the edges connected to it are removed, the tree splits into one or more non-empty subtrees.
- The score of a node is the product of the sizes of all resulting subtrees.
Return the number of nodes with the highest score in the tree.