Find Bottom Left Tree Value
The key idea
The answer is the first node you reach on the deepest level. A level-order (BFS) sweep that records the first node of every row leaves the last row's first node as the answer; a depth-first walk that goes left-first and only updates when it reaches a strictly deeper level lands on the same node.
Problem
Given the root of a binary tree, return the leftmost value in the last row of the tree.
The last row is the deepest level of the tree. If that level holds several nodes, return the value of the one furthest to the left.
Constraints
- The number of nodes in the tree is in the range
[1, 10^4]. -2^31 <= Node.val <= 2^31 - 1
Examples
Input: root = [2,1,3]
Output: 1
Input: root = [1,2,3,4,null,5,6,null,null,7]
Output: 7
Complexity
Time: O(n) Space: O(n)
See the full solution
- ✓Full worked approach
- ✓Reference code in 5 languages
- ✓Problem-solving tips
- ✓Step-by-step animated visualization
More BFS / DFS problems
- 01 MatrixMEDIUM
- Average of Levels in Binary TreeEASY
- Binary Tree Level Order TraversalMEDIUM
- Binary Tree Level Order Traversal IIMEDIUM
- Binary Tree Right Side ViewMEDIUM
- Binary Tree Zigzag Level Order TraversalMEDIUM
- Find Largest Value in Each Tree RowMEDIUM
- Flood FillEASY
- Keys and RoomsMEDIUM
- Max Area of IslandMEDIUM
- Maximum Level Sum of a Binary TreeMEDIUM
- Minimum Depth of Binary TreeEASY