Find Largest Value in Each Tree Row

IF
AlgoAxiomStaff Engineers
JSTS
Medium20 mins

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

Examples

Example 1:

132539

Input: root = [1, 3, 2, 5, 3, null, 9]

Output: [1, 3, 9]

Explanation:

  • Row 0: max(1) = 1
  • Row 1: max(3, 2) = 3
  • Row 2: max(5, 3, 9) = 9

Example 2:

123

Input: root = [1, 2, 3]

Output: [1, 3]

Example 3:

Input: root = []

Output: []

Constraints

  • The number of nodes in the tree is in the range [0, 10⁴]
  • -2³¹ <= Node.val <= 2³¹ - 1
Source: Tree Breadth-First Search pattern — AlgoAxiom
JavaScript
Test Case 1
root = [1, 2, 3]
Test Case 2
root = [1, 2, 3, 4, 5]
Idle