Watch 10 video solutions for Find Largest Value in Each Tree Row, a medium level problem involving Tree, Depth-First Search, Breadth-First Search. This walkthrough by NeetCodeIO has 11,367 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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:
[0, 104].-231 <= Node.val <= 231 - 1Problem Overview: Given the root of a binary tree, return the maximum value found at each depth level. Each row of the tree represents a level, and you need the largest node value from that level before moving to the next.
Approach 1: Breadth-First Search (Level Order Traversal) (Time: O(n), Space: O(w))
The most direct way is a level-order traversal using a queue. Start from the root and process nodes level by level using Breadth-First Search. For each level, iterate through all nodes currently in the queue, track the maximum value seen, and append it to the result list. While processing a node, push its left and right children into the queue so the next iteration processes the next row.
The key insight: BFS naturally groups nodes by depth. Since each queue cycle processes exactly one tree level, calculating the maximum value for that row becomes straightforward. Every node is visited once, giving O(n) time complexity. The queue can hold up to the maximum width of the tree, so space complexity is O(w). This approach is intuitive and commonly expected when solving level-based problems on a binary tree using Breadth-First Search.
Approach 2: Depth-First Search with Level Tracking (Time: O(n), Space: O(h))
A recursive Depth-First Search also works if you track the depth of each node. Traverse the tree using preorder traversal (node, left, right) and maintain a list where index i stores the maximum value for level i. When visiting a node, check if this is the first time reaching that depth. If so, append the value. Otherwise, update the stored value with max(current, node.val).
The key idea: DFS reaches nodes in depth order, and the recursion stack implicitly tracks levels. Instead of processing nodes level by level, you update the result as you discover nodes. Each node contributes exactly once, so time complexity remains O(n). The recursion stack depth is bounded by the height of the tree, giving O(h) space. This technique is useful when you prefer recursion or when solving related problems involving Depth-First Search and depth tracking.
Recommended for interviews: The BFS level-order traversal is usually the first approach interviewers expect because the problem explicitly asks for the largest value per row. BFS directly mirrors that structure and is easier to reason about under pressure. The DFS solution demonstrates deeper understanding of tree traversal patterns and shows that you can convert level-based problems into depth-indexed computations.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Breadth-First Search (Level Order) | O(n) | O(w) | Best when solving level-based tree problems where results depend on rows or layers |
| Depth-First Search with Level Tracking | O(n) | O(h) | Useful when recursion is preferred or when computing values while traversing depth-first |