Skip to content

ORIGINAL CODING QUESTION

Busiest reporting level

A binary org chart stores a signed contribution score on every node. The root is level 0 and each child sits one level below its parent. Add up the scores on each level and return the largest level total. Scores can be negative, so the answer can be negative too.

Open practice workspace

Original exercise · easy. Editorial role relevance does not imply a company has asked this question.

Input and output

n, then n triples (value,leftChild,rightChild). Node 0 is the root; a missing child is written as -1.

Constraints

1 ≤ n ≤ 200; -100 ≤ value ≤ 100; the triples form one connected binary tree and every child ID is larger than its parent ID.

Example 1

Input

3 1 1 2 2 -1 -1 -5 -1 -1

Output

1

Level 0 totals 1 and level 1 totals 2 + (-5) = -3, so the peak is 1.

Example 2

Input

4 -1 1 -1 -2 2 -1 -3 3 -1 -4 -1 -1

Output

-1

Each level holds a single negative score; the best is -1 at the root.

Explain your approach

Explain your time and auxiliary space complexity. Which input makes your algorithm work hardest?

Private grading inputs, expected outputs and canonical reference implementations are withheld. Authored expected outputs were cross-checked with independent reference oracles; compiler/runtime behavior still needs a verified execution provider.

Hint 1

Every node belongs to exactly one level.

Hint 2

A breadth-first pass, or a depth array filled top-down, groups nodes by level.

Hint 3

Start the best total from a real level sum, not from zero.

Practice this question

Save your work privately, explain decisions and return to your revision queue.

Open practice workspaceBrowse the question library →