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.
- The number of nodes in the tree is in the range [1, 10⁴].
- -2³¹ <= Node.val <= 2³¹ - 1
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.
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".
Approach
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).
Two ways to solve it
Read rows right to left; the last node taken is the answer.
- Nothing to track: no depth, no row size.
- No recursion: a 10⁴-node chain is safe.
The shortest correct answer.
Go left before right and record the first node seen at each new depth.
- Small memory: O(h) for a balanced tree.
- Watch out: a chain needs a higher recursion limit in Python.
Pick it if you already think recursively.
Both visit every node once. The right-first BFS needs no depth tracking and no recursion, so the steps, code and live demo follow it; the DFS version comes after the demo.
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.
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.
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.
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.
Code in Python, C++ and Java
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.
Common pitfalls
Queueing the left child first
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.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
node = root
while node.left:
node = node.left
return node.val# 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
if node.left:
answer = node.left.valanswer = 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.
Complexity
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.