FastPrepCount Balanced Nodes in a Rooted Tree
Problems/Count Balanced Nodes in a Rooted Tree

Count Balanced Nodes in a Rooted Tree

DRW logoDRW● MediumNEW GRADINTERNOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The report does not show a limit on the number of nodes, and its second example is cut off. Our version is about 96% the same as the reported question.

You are given a rooted tree represented by an adjacency list subtrees. Node 0 is the root, and subtrees[i] lists the indices of the immediate children of node i.

The size of a subtree is the number of nodes it contains, including the node at its root.

A node is balanced if all of its immediate child subtrees have the same size. A node with zero or one child is balanced.

Return the number of balanced nodes in the tree.

Function

solution(subtrees: int[][]) → int

Examples

Example 1

subtrees = [[1,2],[3,4],[5],[],[],[]]return = 5

Node 0 has child-subtree sizes 3 and 2, so it is not balanced. Every other node is balanced, giving a total of 5.

Example 2

subtrees = [[1,2,3],[],[],[]]return = 4

The root has three child subtrees of size 1. The root and all three leaves are balanced.

Example 3

subtrees = [[1,2],[3],[5,6],[4],[],[],[]]return = 7

The two subtrees below the root both have size 3, even though their shapes differ. Every node is balanced.

Constraints

  • subtrees is non-empty and contains one row for every node.
  • Node 0 is the root.
  • Every child index is a valid index into subtrees.
  • Every node other than node 0 appears exactly once among the child lists, node 0 never appears as a child, and the representation contains no cycle.
  • The order of indices within a child list does not affect the result.

More DRW Online Assessment questions

  • Largest Number With Even Digit FrequenciesOA · Seen Sep 2026
  • Shortest Path Around Rectangular ObstaclesOA · Seen Sep 2026
  • 2020 Account Balance with Monthly Card FeesOA · Seen Aug 2026
  • Nested In-Memory Database TransactionsOA · Seen Aug 2026
  • Password Requirement CheckOA · Seen Aug 2026
  • Remove Excess Consecutive CharactersOA · Seen Aug 2026
  • Last Round for Each PlayerOA · Seen Jul 2026
  • Shortest Compressed Length After RemovalOA · Seen Jul 2026
  • Doctor Appointment Slot AssignmentOA · Seen Jul 2026
  • Maximum Even-Sum Neighboring PairsOA · Seen Jul 2026

Similar questions at other companies

  • Base Unit ConversionsAdobe · OAAdobe interview questions
  • Count Prime Values in SubtreesVisa · OAVisa interview questions
  • Part 1: The Narrowest KeyScale AI · OAScale AI interview questions
  • Sewer Drainage PartitionTwo Sigma · OATwo Sigma interview questions
  • Merge Row Trees by Key PathHudson River Trading · OAHudson River Trading interview questions
  • Chain of CommandIMC Trading · OAIMC Trading interview questions
  • Construct Binary Tree S-ExpressionOptiver · OAOptiver interview questions
  • Chain of CommandUber · OAUber interview questions
  • Maximum Data Transfer TimeMicrosoft · OAMicrosoft interview questions
  • Build and Render an Organization Role TreeStripe · OAStripe interview questions
All DRW interview questionsSee DRW hiring insights
Saved
class Solution {
    public int solution(int[][] subtrees) {
        // Write your code here.
    }
}
Checking account…