Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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.
Example 1:
Input: parents = [-1,2,0,2,0] Output: 3 Explanation: - The score of node 0 is: 3 * 1 = 3 - The score of node 1 is: 4 = 4 - The score of node 2 is: 1 * 1 * 2 = 2 - The score of node 3 is: 4 = 4 - The score of node 4 is: 4 = 4 The highest score is 4, and three nodes (node 1, node 3, and node 4) have the highest score.
Example 2:
Input: parents = [-1,2,0] Output: 2 Explanation: - The score of node 0 is: 2 = 2 - The score of node 1 is: 2 = 2 - The score of node 2 is: 1 * 1 = 1 The highest score is 2, and two nodes (node 0 and node 1) have the highest score.
Constraints:
n == parents.length2 <= n <= 105parents[0] == -10 <= parents[i] <= n - 1 for i != 0parents represents a valid binary tree.Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Tree
[-1,2,0,2,0]
[-1,2,0]
sum-of-distances-in-tree)delete-nodes-and-return-forest)maximum-product-of-splitted-binary-tree)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2049: Count Nodes With the Highest Score
class Solution {
private List<Integer>[] g;
private int ans;
private long mx;
private int n;
public int countHighestScoreNodes(int[] parents) {
n = parents.length;
g = new List[n];
Arrays.setAll(g, i -> new ArrayList<>());
for (int i = 1; i < n; ++i) {
g[parents[i]].add(i);
}
dfs(0, -1);
return ans;
}
private int dfs(int i, int fa) {
int cnt = 1;
long score = 1;
for (int j : g[i]) {
if (j != fa) {
int t = dfs(j, i);
cnt += t;
score *= t;
}
}
if (n - cnt > 0) {
score *= n - cnt;
}
if (mx < score) {
mx = score;
ans = 1;
} else if (mx == score) {
++ans;
}
return cnt;
}
}
// Accepted solution for LeetCode #2049: Count Nodes With the Highest Score
func countHighestScoreNodes(parents []int) (ans int) {
n := len(parents)
g := make([][]int, n)
for i := 1; i < n; i++ {
g[parents[i]] = append(g[parents[i]], i)
}
mx := 0
var dfs func(i, fa int) int
dfs = func(i, fa int) int {
cnt, score := 1, 1
for _, j := range g[i] {
if j != fa {
t := dfs(j, i)
cnt += t
score *= t
}
}
if n-cnt > 0 {
score *= n - cnt
}
if mx < score {
mx = score
ans = 1
} else if mx == score {
ans++
}
return cnt
}
dfs(0, -1)
return
}
# Accepted solution for LeetCode #2049: Count Nodes With the Highest Score
class Solution:
def countHighestScoreNodes(self, parents: List[int]) -> int:
def dfs(i: int, fa: int):
cnt = score = 1
for j in g[i]:
if j != fa:
t = dfs(j, i)
score *= t
cnt += t
if n - cnt:
score *= n - cnt
nonlocal ans, mx
if mx < score:
mx = score
ans = 1
elif mx == score:
ans += 1
return cnt
n = len(parents)
g = [[] for _ in range(n)]
for i in range(1, n):
g[parents[i]].append(i)
ans = mx = 0
dfs(0, -1)
return ans
// Accepted solution for LeetCode #2049: Count Nodes With the Highest Score
/**
* [2049] Count Nodes With the Highest Score
*
* 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.
*
* Example 1:
* <img alt="example-1" src="https://assets.leetcode.com/uploads/2021/10/03/example-1.png" style="width: 604px; height: 266px;" />
* Input: parents = [-1,2,0,2,0]
* Output: 3
* Explanation:
* - The score of node 0 is: 3 * 1 = 3
* - The score of node 1 is: 4 = 4
* - The score of node 2 is: 1 * 1 * 2 = 2
* - The score of node 3 is: 4 = 4
* - The score of node 4 is: 4 = 4
* The highest score is 4, and three nodes (node 1, node 3, and node 4) have the highest score.
*
* Example 2:
* <img alt="example-2" src="https://assets.leetcode.com/uploads/2021/10/03/example-2.png" style="width: 95px; height: 143px;" />
* Input: parents = [-1,2,0]
* Output: 2
* Explanation:
* - The score of node 0 is: 2 = 2
* - The score of node 1 is: 2 = 2
* - The score of node 2 is: 1 * 1 = 1
* The highest score is 2, and two nodes (node 0 and node 1) have the highest score.
*
*
* 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.
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/count-nodes-with-the-highest-score/
// discuss: https://leetcode.com/problems/count-nodes-with-the-highest-score/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn count_highest_score_nodes(parents: Vec<i32>) -> i32 {
0
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[ignore]
fn test_2049_example_1() {
let parents = vec![-1, 2, 0, 2, 0];
let result = 3;
assert_eq!(Solution::count_highest_score_nodes(parents), result);
}
#[test]
#[ignore]
fn test_2049_example_2() {
let parents = vec![-1, 2, 0];
let result = 2;
assert_eq!(Solution::count_highest_score_nodes(parents), result);
}
}
// Accepted solution for LeetCode #2049: Count Nodes With the Highest Score
function countHighestScoreNodes(parents: number[]): number {
const n = parents.length;
const g: number[][] = Array.from({ length: n }, () => []);
for (let i = 1; i < n; i++) {
g[parents[i]].push(i);
}
let [ans, mx] = [0, 0];
const dfs = (i: number, fa: number): number => {
let [cnt, score] = [1, 1];
for (const j of g[i]) {
if (j !== fa) {
const t = dfs(j, i);
cnt += t;
score *= t;
}
}
if (n - cnt) {
score *= n - cnt;
}
if (mx < score) {
mx = score;
ans = 1;
} else if (mx === score) {
ans++;
}
return cnt;
};
dfs(0, -1);
return ans;
}
Use this to step through a reusable interview workflow for this problem.
BFS with a queue visits every node exactly once — O(n) time. The queue may hold an entire level of the tree, which for a complete binary tree is up to n/2 nodes = O(n) space. This is optimal in time but costly in space for wide trees.
Every node is visited exactly once, giving O(n) time. Space depends on tree shape: O(h) for recursive DFS (stack depth = height h), or O(w) for BFS (queue width = widest level). For balanced trees h = log n; for skewed trees h = n.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Recursive traversal assumes children always exist.
Usually fails on: Leaf nodes throw errors or create wrong depth/path values.
Fix: Handle null/base cases before recursive transitions.