Skip to content

Practice Count Nodes With the Highest Score

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#2049
LeetCode Problem

Count Nodes With the Highest Score

Problem Statement

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.

Source: Count Nodes With the Highest Score on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
parents = [-1,2,0,2,0]
Output
3

# Example 2

Input
parents = [-1,2,0]
Output
2

Constraints

n == parents.length
2 <= n <= 10^5
parents[0] == -1
0 <= parents[i] <= n - 1 for i != 0
parents represents a valid binary tree.

Related LeetCode Problems

Practice more problems similar to Count Nodes With the Highest Score.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.