Find Largest Value in Each Tree Row
MEDIUMDescription
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]
Example 2:
Input: root = [1,2,3] Output: [1,3]
Constraints:
- The number of nodes in the tree will be in the range
[0, 104]. -231 <= Node.val <= 231 - 1
Approaches
Checkout 2 different approaches to solve Find Largest Value in Each Tree Row. Click on different approaches to view the approach and algorithm in detail.
Breadth-First Search (Level Order Traversal)
This approach traverses the tree level by level using a queue. For each level, it iterates through all the nodes at that level to find the maximum value, then adds it to the result list. This is a very intuitive method for problems involving tree levels.
Algorithm
- Initialize an empty list
result. - If the
rootis null, return the empty list. - Create a
Queueand add therootnode. - While the queue is not empty:
- Get the number of nodes in the current level,
levelSize = queue.size(). - Initialize a variable
maxInLeveltoInteger.MIN_VALUE. - Loop
levelSizetimes:- Dequeue a node
currentNode. - Update
maxInLevelwith the maximum of its current value andcurrentNode.val. - Enqueue the left and right children of
currentNodeif they are not null.
- Dequeue a node
- Add
maxInLevelto theresultlist.
- Get the number of nodes in the current level,
- Return
result.
We can solve this problem by performing a level order traversal of the tree, which is naturally implemented using a Breadth-First Search (BFS) algorithm with a queue.
The core idea is to process the tree one level at a time. We start by putting the root node in a queue. Then, we enter a loop that continues as long as the queue is not empty. In each iteration of this outer loop, we are processing a single level. We first determine the number of nodes on the current level by checking the queue's size. We then iterate exactly that many times, dequeueing one node at a time. While processing the nodes of a level, we keep track of the maximum value seen so far. After the inner loop finishes, we have the largest value for that level, which we add to our result list. We also add the children of each processed node to the queue, which sets up the next level for the subsequent iteration of the outer loop.
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<Integer> largestValues(TreeNode root) {
List<Integer> result = new ArrayList<>();
if (root == null) {
return result;
}
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()) {
int levelSize = queue.size();
int maxInLevel = Integer.MIN_VALUE;
for (int i = 0; i < levelSize; i++) {
TreeNode currentNode = queue.poll();
maxInLevel = Math.max(maxInLevel, currentNode.val);
if (currentNode.left != null) {
queue.offer(currentNode.left);
}
if (currentNode.right != null) {
queue.offer(currentNode.right);
}
}
result.add(maxInLevel);
}
return result;
}
}
Complexity Analysis
Pros and Cons
- Very intuitive for level-by-level tree problems.
- Iterative approach avoids recursion depth limits and potential stack overflow on very deep trees.
- Can be less space-efficient than DFS for wide trees (e.g., complete binary trees), as the queue can hold up to O(N) nodes.
Code Solutions
Checking out 3 solutions in different languages for Find Largest Value in Each Tree Row. Click on different languages to view the code.
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */ class Solution { public List < Integer > largestValues ( TreeNode root ) { List < Integer > ans = new ArrayList <>(); if ( root == null ) { return ans ; } Deque < TreeNode > q = new ArrayDeque <>(); q . offer ( root ); while (! q . isEmpty ()) { int t = q . peek (). val ; for ( int i = q . size (); i > 0 ; -- i ) { TreeNode node = q . poll (); t = Math . max ( t , node . val ); if ( node . left != null ) { q . offer ( node . left ); } if ( node . right != null ) { q . offer ( node . right ); } } ans . add ( t ); } return ans ; } }Video Solution
Watch the video walkthrough for Find Largest Value in Each Tree Row
Similar Questions
5 related questions you might find useful
Algorithms:
Data Structures:
Companies:
Subscribe to Scale Engineer newsletter
Learn about System Design, Software Engineering, and interview experiences every week.
No spam, unsubscribe at any time.