Find Largest Value in Each Tree Row - Problem
Find the Champion of Each Level
You're given the
For example, if you have a tree with multiple levels, you need to:
• Examine all nodes at level 0 (root level) and find the maximum
• Examine all nodes at level 1 and find the maximum
• Continue for all levels until you reach the leaves
Return an array where
Note: The tree levels are 0-indexed, starting from the root.
You're given the
root of a binary tree and need to find the largest value in each row of the tree. Think of it as finding the "champion" node at each level of the tree hierarchy.For example, if you have a tree with multiple levels, you need to:
• Examine all nodes at level 0 (root level) and find the maximum
• Examine all nodes at level 1 and find the maximum
• Continue for all levels until you reach the leaves
Return an array where
result[i] contains the largest value found at level i of the tree.Note: The tree levels are 0-indexed, starting from the root.
Input & Output
example_1.py — Basic Binary Tree
$
Input:
root = [1,3,2,5,3,null,9]
›
Output:
[1,3,9]
💡 Note:
Level 0: only node 1, so max = 1. Level 1: nodes 3 and 2, so max = 3. Level 2: nodes 5, 3, and 9, so max = 9.
example_2.py — Single Node Tree
$
Input:
root = [1]
›
Output:
[1]
💡 Note:
Tree has only one level (level 0) with a single node containing value 1.
example_3.py — Empty Tree
$
Input:
root = []
›
Output:
[]
💡 Note:
Empty tree has no levels, so return empty array.
Constraints
- The number of nodes in tree is in range [0, 104]
- -231 ≤ Node.val ≤ 231 - 1
- Tree can be empty (null root)
Visualization
Tap to expand
Understanding the Visualization
1
Level 0 Tournament
Only one participant (root), automatic champion with value 1
2
Level 1 Tournament
Two participants (3 vs 2), champion is 3
3
Level 2 Tournament
Three participants (5 vs 3 vs 9), champion is 9
4
Collect Champions
Final result: [1, 3, 9] representing each level's champion
Key Takeaway
🎯 Key Insight: BFS naturally processes nodes level by level, making it perfect for finding the maximum value at each tree level in a single efficient pass.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code