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.

Visualization

Tap to expand
๐Ÿข Corporate Building SearchFloor 3 (Level 0)Office 2Floor 2 (Level 1)Office 1Office 3Floor 1 (Level 2) - Bottom FloorOffice 7 โœ“Office 4๐Ÿ›—BFSElevator๐ŸŽฏ Found: Office 7 is the leftmost on the bottom floor!
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.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Visit each node once to collect data, then scan through collected nodes

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

Store information for all nodes in the tree

n
2n
โšก Linearithmic Space

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
Asked in
Amazon 15 Microsoft 12 Facebook 8 Google 6
89.0K Views
Medium Frequency
~15 min Avg. Time
2.3K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen