Find Bottom Left Tree Value (LeetCode 513): Solution and Explanation
Given a binary tree, return the value of the leftmost node in its deepest row. The tree has at least one node, so there is always an answer.
Intuition
A normal level-order traversal reads each row from left to right. You could record the first node of each row and return the one recorded last, but that needs extra bookkeeping per row.
Flip the order inside each row instead: enqueue the right child before the left child. Rows still leave the queue top to bottom, but each row now leaves from right to left.
The last node to leave the queue is then in the deepest row, and within that row it is the leftmost node. So the answer is simply whatever node was dequeued last.
Approach: Right-to-left breadth-first search
- Put the root in a queue and set
node = root. - While the queue is not empty, dequeue the front into
node. - Enqueue
node.rightif it exists, thennode.leftif it exists. - When the queue empties, return
node.val.
Step-by-step example
root = [2,1,3]
- The queue starts as [2].
- Dequeue 2. Enqueue its right child 3, then its left child 1. The queue is [3, 1].
- Dequeue 3, a leaf. The queue is [1].
- Dequeue 1, a leaf. The queue is empty.
- The last node dequeued was 1, the leftmost node of the bottom row. Return 1.
Code: Python, Java, C++
1class Solution:
2 def findBottomLeftValue(self, root: Optional[TreeNode]) -> int:
3 queue = deque([root])
4 node = root
5 while queue:
6 node = queue.popleft()
7 if node.right:
8 queue.append(node.right)
9 if node.left:
10 queue.append(node.left)
11 return node.val1class Solution {
2 public int findBottomLeftValue(TreeNode root) {
3 Queue<TreeNode> queue = new ArrayDeque<>();
4 queue.offer(root);
5 TreeNode node = root;
6 while (!queue.isEmpty()) {
7 node = queue.poll();
8 if (node.right != null) {
9 queue.offer(node.right);
10 }
11 if (node.left != null) {
12 queue.offer(node.left);
13 }
14 }
15 return node.val;
16 }
17}1class Solution {
2public:
3 int findBottomLeftValue(TreeNode* root) {
4 queue<TreeNode*> q;
5 q.push(root);
6 TreeNode* node = root;
7 while (!q.empty()) {
8 node = q.front();
9 q.pop();
10 if (node->right) {
11 q.push(node->right);
12 }
13 if (node->left) {
14 q.push(node->left);
15 }
16 }
17 return node->val;
18 }
19};Time and space complexity
O(n)
Each node is enqueued and dequeued exactly once.
O(w)
The queue holds at most one full row plus part of the next, where w is the widest row (up to about n / 2).
Other approaches
Left-to-right BFS remembering each row's first node (time O(n), space O(w))
Process the queue one row at a time and store the first node of every row. The value stored for the final row is the answer.
Depth-first search tracking the deepest depth (time O(n), space O(h))
Visit the left child before the right and record a node's value only when its depth is greater than any seen so far. The first node reached at each new depth is the leftmost one.
Common mistakes
- Returning the leftmost node of the whole tree, found by following left pointers. The deepest row can sit under a right subtree, as in [1,2,3,null,null,4].
- Enqueueing left before right and returning the last node, which gives the bottom-right value instead.
- In the DFS version, updating on depth >= best instead of depth > best, which lets later nodes on the same row overwrite the leftmost one.
Frequently asked questions
Why is the last dequeued node guaranteed to be in the deepest row?
BFS dequeues every node of row d before any node of row d + 1. So the final node out must belong to the deepest row.
Does the bottom-left node have to be a left child?
No. It is just the first node of the last row. It can be a right child, or even the only node on that row.
Which version uses less memory?
BFS stores up to a full row, which is large for wide trees. DFS stores one root-to-leaf path, which is large for tall, thin trees. Both are O(n) in the worst case.