0% completed
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
Solution
To solve this problem, we need to determine the maximum value at each level (row) of the binary tree. A level order traversal (breadth-first search) is ideal for this task. By traversing each level of the tree one by one, we can find the maximum value in each row and add it to the result list. This approach ensures that every level is fully processed before moving to the next.
Using level order traversal is efficient because it only requires a single pass through the tree, processing each node exactly once. This makes it an optimal solution with a linear time complexity, proportional to the number of nodes in the tree.
Step-by-Step Algorithm
-
Initialize Result List:
- Create
result
as an empty list to store the largest values of each row.
- Create
-
Check for Empty Tree:
- If
root
isnull
, return the emptyresult
list.
- If
-
Initialize Queue:
- Create a queue and add
root
to start level order traversal.
- Create a queue and add
-
Process Each Level:
- While the queue is not empty:
- Determine the number of nodes at the current level (
levelSize
). - Set
maxVal
toInteger.MIN_VALUE
.
- Determine the number of nodes at the current level (
- While the queue is not empty:
-
Process Each Node at Current Level:
- For each node in the current level:
- Dequeue a node, update
maxVal
with the maximum ofmaxVal
and the node's value. - If the node has a left child, enqueue it.
- If the node has a right child, enqueue it.
- Dequeue a node, update
- For each node in the current level:
-
Store Largest Value:
- After processing all nodes at the current level, add
maxVal
toresult
.
- After processing all nodes at the current level, add
-
Repeat for All Levels:
- Continue until the queue is empty.
-
Return Result:
- Return
result
containing the largest values in each row.
- Return
Algorithm Walkthrough
Input: root = [1, 2, 3, 4, 5, null, 6]
-
Initialize:
result = []
(to store largest values)queue = [1]
(start with the root node)
-
Level 1:
levelSize = 1
(1 node at this level),maxVal = -∞
(initialize to smallest value)- Process Node 1:
- Dequeue node
1
→queue = []
maxVal = max(-∞, 1) = 1
(update because-∞
is less than1
)- Enqueue children
2
and3
→queue = [2, 3]
- Dequeue node
- Store Largest Value of Level 1:
- Add
1
toresult
→result = [1]
- Add
-
Level 2:
levelSize = 2
(2 nodes at this level),maxVal = -∞
(reset for new level)- Process Node 2:
- Dequeue node
2
→queue = [3]
maxVal = max(-∞, 2) = 2
(update because-∞
is less than2
)- Enqueue children
4
and5
→queue = [3, 4, 5]
- Dequeue node
- Process Node 3:
- Dequeue node
3
→queue = [4, 5]
maxVal = max(2, 3) = 3
(update because3
is greater than2
)- Enqueue child
6
→queue = [4, 5, 6]
- Dequeue node
- Store Largest Value of Level 2:
- Add
3
toresult
→result = [1, 3]
- Add
-
Level 3:
levelSize = 3
(3 nodes at this level),maxVal = -∞
(reset for new level)- Process Node 4:
- Dequeue node
4
→queue = [5, 6]
maxVal = max(-∞, 4) = 4
(update because-∞
is less than4
)
- Dequeue node
- Process Node 5:
- Dequeue node
5
→queue = [6]
maxVal = max(4, 5) = 5
(update because5
is greater than4
)
- Dequeue node
- Process Node 6:
- Dequeue node
6
→queue = []
maxVal = max(5, 6) = 6
(update because6
is greater than5
)
- Dequeue node
- Store Largest Value of Level 3:
- Add
6
toresult
→result = [1, 3, 6]
- Add
-
Return Result:
- Final output:
[1, 3, 6]
(largest values from each row).
- Final output:
Code
Complexity Analysis
Time Complexity
- The algorithm performs a level-order traversal (BFS) of the binary tree. Each node in the tree is visited exactly once, and all nodes are added to the queue at most once.
- Therefore, the time complexity is O(n), where
n
is the number of nodes in the binary tree.
Space Complexity
- The space complexity is mainly determined by the queue used for the level-order traversal. In the worst case, the queue could hold all nodes at the deepest level of the tree. For a balanced binary tree, the maximum number of nodes at the deepest level is approximately
n/2
, wheren
is the total number of nodes. - Therefore, the space complexity is O(n) in the worst case.
Table of Contents
Problem Statement
Examples
Example 1
Solution
Step-by-Step Algorithm
Algorithm Walkthrough
Code
Complexity Analysis
Time Complexity
Space Complexity