java.lang.Object
g2001_2100.s2049_count_nodes_with_the_highest_score.Solution

public class Solution extends Object
2049 - Count Nodes With the Highest Score.<p>Medium</p> <p>There is a <strong>binary</strong> tree rooted at <code>0</code> consisting of <code>n</code> nodes. The nodes are labeled from <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> integer array <code>parents</code> representing the tree, where <code>parents[i]</code> is the parent of node <code>i</code>. Since node <code>0</code> is the root, <code>parents[0] == -1</code>.</p> <p>Each node has a <strong>score</strong>. To find the score of a node, consider if the node and the edges connected to it were <strong>removed</strong>. The tree would become one or more <strong>non-empty</strong> subtrees. The <strong>size</strong> of a subtree is the number of the nodes in it. The <strong>score</strong> of the node is the <strong>product of the sizes</strong> of all those subtrees.</p> <p>Return <em>the <strong>number</strong> of nodes that have the <strong>highest score</strong></em>.</p> <p><strong>Example 1:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2021/10/03/example-1.png" alt="example-1" /></p> <p><strong>Input:</strong> parents = [-1,2,0,2,0]</p> <p><strong>Output:</strong> 3</p> <p><strong>Explanation:</strong></p> <ul> <li> <p>The score of node 0 is: 3 * 1 = 3</p> </li> <li> <p>The score of node 1 is: 4 = 4</p> </li> <li> <p>The score of node 2 is: 1 * 1 * 2 = 2</p> </li> <li> <p>The score of node 3 is: 4 = 4</p> </li> <li> <p>The score of node 4 is: 4 = 4</p> </li> </ul> <p>The highest score is 4, and three nodes (node 1, node 3, and node 4) have the highest score.</p> <p><strong>Example 2:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2021/10/03/example-2.png" alt="example-2" /></p> <p><strong>Input:</strong> parents = [-1,2,0]</p> <p><strong>Output:</strong> 2</p> <p><strong>Explanation:</strong></p> <ul> <li> <p>The score of node 0 is: 2 = 2</p> </li> <li> <p>The score of node 1 is: 2 = 2</p> </li> <li> <p>The score of node 2 is: 1 * 1 = 1</p> </li> </ul> <p>The highest score is 2, and two nodes (node 0 and node 1) have the highest score.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == parents.length</code></li> <li><code>2 <= n <= 10<sup>5</sup></code></li> <li><code>parents[0] == -1</code></li> <li><code>0 <= parents[i] <= n - 1</code> for <code>i != 0</code></li> <li><code>parents</code> represents a valid binary tree.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • countHighestScoreNodes

      public int countHighestScoreNodes(int[] parents)