Tap here to switch tabs
Problem
Submissions

Problem: Count Nodes With the Highest Score

med
30 min
Try to solve the Count Nodes With the Highest Score problem.

Statement

You are given a 00 indexed integer array parents of length n, representing a binary tree rooted at node 00, where parents[i] is the parent of node i. Since node 00 is the root, parents[0] is −1-1.

Each node has a score. To compute the score of a node:

  1. Remove the node and all of its connected edges from the tree.

  2. The remaining nodes will form one or more non empty subtrees.

  3. The score of the node is the product of the sizes of all these remaining subtrees. If there are no remaining subtrees, the score is 00.

Return the number of nodes that achieve the maximum possible score.

Constraints:

  • n ==== parents.length

  • 2≤2 \leq n ≤105\leq 10^5

  • parents[0] ==−1== -1

  • 0≤0 \leq parents[i] ≤\leq n −1- 1 for all i ≠0\neq 0

  • parents represents a valid binary tree.

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Count Nodes With the Highest Score

med
30 min
Try to solve the Count Nodes With the Highest Score problem.

Statement

You are given a 00 indexed integer array parents of length n, representing a binary tree rooted at node 00, where parents[i] is the parent of node i. Since node 00 is the root, parents[0] is −1-1.

Each node has a score. To compute the score of a node:

  1. Remove the node and all of its connected edges from the tree.

  2. The remaining nodes will form one or more non empty subtrees.

  3. The score of the node is the product of the sizes of all these remaining subtrees. If there are no remaining subtrees, the score is 00.

Return the number of nodes that achieve the maximum possible score.

Constraints:

  • n ==== parents.length

  • 2≤2 \leq n ≤105\leq 10^5

  • parents[0] ==−1== -1

  • 0≤0 \leq parents[i] ≤\leq n −1- 1 for all i ≠0\neq 0

  • parents represents a valid binary tree.