LeetCode #513 Medium

Find Bottom Left Tree Value

Find Bottom Left Tree Value is LeetCode 513 (Medium). Given the root of a binary tree, return the value of the leftmost node in its last row: among the nodes at the greatest depth, the one furthest to the left.

  • The tree has at least one node, so there is always an answer.
  • It can have up to 10⁴ nodes, and a tree shaped like a chain is that many rows deep.
Constraints
  • The number of nodes in the tree is in the range [1, 10⁴].
  • -2³¹ <= Node.val <= 2³¹ - 1
treebfs
Open on LeetCode ↗
02

Intuition

BFS reads a tree row by row, top to bottom, so the last node it takes always comes from the bottom row. Which node of that row comes last depends on the order inside each row:

  • Left child first, the usual BFS: every row is read left to right, so the last node taken is the bottom row's rightmost node.
  • Right child first: every row is read right to left, so the last node taken is the bottom row's leftmost node, which is the answer.

No row counting is needed. One variable holding the node taken most recently is enough.

How to spot this pattern

Any question about the first or last node of a row, or the deepest row, suggests BFS, because BFS already hands you the rows in order. Changing the order children are queued in is often all it takes to turn "rightmost" into "leftmost".

03

Approach

Try it first

Before reading on: draw [1, 2, 3, null, null, 4] and mark its leftmost node in the bottom row. Does walking down left children from the root reach it? Then list the order a normal BFS takes the nodes, and work out how to make the answer come out last. Aim for O(n).

1

Start the queue with the root

The tree is never empty, so the root goes straight in. The queue holds the nodes that have been found but not yet read, oldest at the front.

2

Take the front node

Pop the front of the queue into node. Every node of one row is queued before any node of the next row, so rows leave the queue in order, top to bottom.

3

Queue the right child, then the left

Push node.right before node.left, skipping a missing child. This reverses the order inside every row, so the leftmost node of a row is always the last of that row to leave the queue.

4

Return the last node taken

When the queue is empty, node is the final node read. It came from the bottom row, and within that row it is the leftmost, so return node.val.

04

Code in Python, C++ and Java

▶1from collections import deque
▶2 
▶3class Solution:
▶4 def findBottomLeftValue(self, root: Optional[TreeNode]) -> int:
▶5 queue = deque([root])
▶6 while queue:
▶7 node = queue.popleft()
▶8 if node.right:
▶9 queue.append(node.right)
▶10 if node.left:
▶11 queue.append(node.left)
▶12 return node.val
row 0row 1213queue, front first2last takenqueue the root 2
queue2
The answer lives in the shaded bottom row, at its left end. BFS takes the tree one row at a time, top to bottom, so the bottom row is always taken last. If each row is also taken right to left, the very last node taken is the bottom row's leftmost node.
row 0row 1213queue, front firstemptylast taken2take 2 from the front
node2row 0
queueempty
Take 2 from the front of the queue; it is now the last node taken. Its children go to the back, right child first, so this row's order is reversed in the next row.
row 0row 1213queue, front first3last taken2queue right child 3 first
queue3
The right child 3 goes in before the left one. Within its row it will be taken earlier than anything to its left, which is what pushes the leftmost node of each row to the end.
row 0row 1213queue, front first31last taken2queue left child 1 after it
queue3, 1
The left child 1 goes in after its sibling, so it is taken later than every node to its right in the same row.
row 0row 1213queue, front first1last taken3take 3 from the front
node3row 1
queue1
Take 3 from the front of the queue; it is now the last node taken. It has no children, so nothing is queued.
row 0row 1213queue, front firstemptylast taken1take 1 from the front
node1row 1
queueempty
Take 1 from the front of the queue; it is now the last node taken. It has no children, so nothing is queued.
row 0row 1213queue, front firstemptylast taken1queue empty → answer 1
answer1last node taken
The queue is empty. The last node taken came from the bottom row, because rows are taken top to bottom, and it is that row's leftmost node, because each row was taken right to left. The answer is 1.
05

DFS with depth

Visit the left subtree before the right one and carry the depth. The first node reached at any depth is the leftmost node of that row, because everything to its left was visited earlier. So record a node only when its depth is strictly greater than the deepest seen so far; with >=, later nodes further right in the same row would overwrite it. A chain of 10⁴ nodes nests 10⁴ calls, so the Python version raises the recursion limit.

▶1import sys
▶2 
▶3class Solution:
▶4 def findBottomLeftValue(self, root: Optional[TreeNode]) -> int:
▶5 sys.setrecursionlimit(20000)
▶6 best = [-1, root.val]
▶7 
▶8 def dfs(node, depth):
▶9 if not node:
▶10 return
▶11 if depth > best[0]:
▶12 best[0], best[1] = depth, node.val
▶13 dfs(node.left, depth + 1)
▶14 dfs(node.right, depth + 1)
▶15 
▶16 dfs(root, 0)
▶17 return best[1]
06

Common pitfalls

Queueing the left child first

✗ Wrong
if node.left:
    queue.append(node.left)
if node.right:
    queue.append(node.right)
✓ Right
if node.right:
    queue.append(node.right)
if node.left:
    queue.append(node.left)

On [2, 1, 3] the code returns 3, the rightmost node of the bottom row, but the answer is 1.

Walking down left children only

✗ Wrong
node = root
while node.left:
    node = node.left
return node.val
✓ Right
# read every row; the bottom row decides

The bottom row can hang off a right branch. On [1, 2, 3, null, null, 4] the walk stops at 2, in row 1, but the bottom row is row 2 and holds only 4.

Remembering only nodes that are left children

✗ Wrong
if node.left:
    answer = node.left.val
✓ Right
answer = node.val  # every node taken, right child queued first

That finds the deepest left leaf, a different question. The answer can be a right child: on [1, 2, 3, null, null, null, 4] the bottom row holds only 4, the right child of 3, but the code returns 2.

07

Complexity

Time
O(n)
Space
O(w)
Every node is queued once and taken once. The queue holds at most one row plus part of the next, so space is O(w), where w is the widest row: about n / 2 for a full tree, 1 for a chain.
08

Find Bottom Left Tree Value FAQ

How is this different from the left view of a binary tree?

The left view lists the leftmost node of every row, from top to bottom. This problem wants only the bottom row's leftmost node, which is the last entry of the left view.

Can I use a normal BFS and keep the first node of each row instead?

Yes. Read the queue size at the start of each row, process exactly that many nodes, and save the first one; after the last row, the saved value is the answer. It is the same O(n), but needs the row-size loop that the right-first version avoids.