Back to course home
0% completed
Find Largest Value in Each Tree Row (medium)
Problem Statement
Given the root
of a binary tree, return an array containing the largest value in each row of the tree (0-indexed).
Examples
Example 1
- Input: root = [1, 2, 3, 4, 5, null, 6]
- Expected Output: [1, 3, 6]
- Justification:
- The first row contains
1
. The largest value is1
. - The second row has
2
and3
, and the largest is3
. - The third row has
4
,5
, and6
, and the largest is6
.
- The first row contains
Example 2
- Input: root = [7, 4, 8, 2, 5, null, 9, null, 3]
- Expected Output: [7, 8, 9, 3]
- Justification:
- The first row contains
7
, and the largest value is7
. - The second row has
4
and8
, and the largest is8
. - The third row has
2
,5
, and9
, and the largest is9
. - The fourth row has
3
, and the largest is3
.
- The first row contains
Example 3
- Input: root = [10, 5]
- Expected Output: [10, 5]
- Justification:
- The first row has
10
, and the largest value is10
. - The second row contains
5
, and the largest is5
.
- The first row has
Constraints:
- The number of nodes in the tree will be in the range [0, 10<sup>4</sup>].
- -2<sup>31</sup> <= Node.val <= 2<sup>31</sup> - 1
Try it yourself
Try solving this question here:
Python3
Python3
. . . .
Mark as Completed
Table of Contents
Problem Statement
Examples
Example 1
Try it yourself