Find Bottom Left Tree Value - Problem
Given the root of a binary tree, you need to find the leftmost value in the bottom row of the tree.
Think of it as looking at a binary tree from above and wanting to know what value sits at the leftmost position of the deepest level. The tree is guaranteed to have at least one node, so there will always be a bottom-left value to find.
Goal: Return the leftmost value in the last row of the binary tree.
Input: Root node of a binary tree
Output: Integer value of the leftmost node in the deepest level
Example: In a tree where the bottom row contains [7, 4], return 7 since it's the leftmost value in that row.
Input & Output
example_1.py โ Basic Binary Tree
$
Input:
[2,1,3,7,4]
โบ
Output:
7
๐ก Note:
The tree has levels: Level 0: [2], Level 1: [1,3], Level 2: [7,4]. The leftmost value in the last row is 7.
example_2.py โ Single Node
$
Input:
[1]
โบ
Output:
1
๐ก Note:
The tree has only one node, so the leftmost value in the bottom row is 1.
example_3.py โ Left-Heavy Tree
$
Input:
[1,2,3,4,null,5,6,null,null,7]
โบ
Output:
7
๐ก Note:
The tree has multiple levels with the deepest level containing only node 7, making it the leftmost (and only) value in the bottom row.
Constraints
- The number of nodes in the tree is in the range [1, 104]
- -231 โค Node.val โค 231 - 1
- It's guaranteed that the tree has at least one node
Visualization
Tap to expand
Understanding the Visualization
1
Enter Building
Start at the lobby (root) with your elevator (queue)
2
Visit Each Floor
Process all offices on each floor from left to right
3
Track Leftmost Office
Remember the first office visited on each floor
4
Reach Bottom Floor
The last floor processed gives us the leftmost office
Key Takeaway
๐ฏ Key Insight: BFS naturally processes nodes level by level from left to right, so the first node we encounter at each level is guaranteed to be the leftmost node at that level.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code