Find Largest Value in Each Tree Row

Given the root of a binary tree, return an array of the largest value in each row of the tree (0-indexed).

30:00

Find Largest Value in Each Tree Row
medium
Free written solution
Topics
Companies

Given the root of a binary tree, return an array of the largest value in each row of the tree (0-indexed).

Example 1:
Input: {"root":[1,3,2,5,3,null,9]}
Output: [1,3,9]
Constraints:
  • The number of nodes in the tree will be in the range [0,104][0, 10^4].

  • −231≤Node.val≤231−1-2^{31} \leq \text{Node.val} \leq 2^{31} - 1

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"root":[1,3,2,5,3,null,9]}

Start with level = [1]: row 0 holds only the root.

132539r0?r1?r2?

level

1

next

empty

result = []

row being scanned
best so far
queued for next row
row maximum
Variables
VariableValue
row-
level size1
best-
values[]
DepthFunction Call
Stack empty
0/17