Find Bottom Left Tree Value - Problem

Given the root of a binary tree, return the leftmost value in the last row of the tree.

You need to find the value of the leftmost node in the bottom-most level of the tree. If there are multiple nodes at the same level, return the leftmost one.

Note: The tree is guaranteed to be non-empty.

Input & Output

Example 1 — Standard Tree
$ Input: root = [2,1,3,4,null,5,6,null,null,7]
Output: 7
💡 Note: The bottom level has one node: 7. This is the leftmost (and only) value in the last row.
Example 2 — Multiple Bottom Nodes
$ Input: root = [1,2,3,4,null,5,6]
Output: 4
💡 Note: The bottom level has nodes [4,5,6]. The leftmost value is 4.
Example 3 — Single Node
$ Input: root = [1]
Output: 1
💡 Note: Only one node exists, so it's the leftmost value in the last (and only) row.

Constraints

  • The number of nodes in the tree is in the range [1, 104]
  • -231 ≤ Node.val ≤ 231 - 1

Visualization

Tap to expand
Find Bottom Left Tree Value INPUT 2 1 3 4 5 6 7 L0 L1 L2 L3 Input Array: [2,1,3,4,null,5,6,null,null,7] Bottom row = Level 3 ALGORITHM (BFS) 1 Initialize Queue Add root to queue 2 2 Process Each Level Track first node per level 1 3 3 Continue BFS L2: [4,5,6], L3: [7] 4 5 6 4 Last Level First Bottom-left = 7 7 -- leftmost! BFS: 2 --> 1,3 --> 4,5,6 --> 7 First of last level = Answer FINAL RESULT 2 1 3 4 5 6 7 Bottom Left! Output: 7 OK - Leftmost at Level 3 Key Insight: BFS traverses level by level, left to right. Track the first node of each level during traversal. When BFS completes, the first node of the last level visited is the bottom-left value. Time: O(n), Space: O(w) TutorialsPoint - Find Bottom Left Tree Value | BFS Approach
Asked in
Google 15 Amazon 12 Microsoft 10 Apple 8
180.0K Views
Medium Frequency
~15 min Avg. Time
2.2K 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