Find Bottom Left Tree Value
Medium
Topics
Given the root of a binary tree, return the leftmost value in the last (deepest) row of the tree.
Example 1
null marks a missing child.Input: root = [2,1,3] Output: 1
Example 2
Input: root = [1,2,3,4,null,5,6,null,null,7] Output: 7
Constraints
- 1 <= number of nodes <= 10^4
- -2^31 <= Node.val <= 2^31 - 1
Editorial — intuition, approach, complexity and pitfalls
Intuition
We want the leftmost value on the deepest level. A DFS can do it by tracking depth and recording the first node seen at each new maximum depth — "first" because a preorder DFS visits left before right.
A level-order BFS is simpler to reason about: the first node of each level is its leftmost, so record it at the start of every level and the last recording to survive is the answer. No depth comparison at all.
An even shorter variant: enqueue the right child before the left, and the last node dequeued overall is the bottom-left. Cute, and harder to read — the per-level recording states the intent plainly.
Approach
- Queue with the root; remember the front value at the start of each level.
- Process exactly one level per outer iteration.
- Return the last recorded value.
Dry Run
tree 1
/ \
2 3
/ / \
4 5 6
/
7
level 0 front 1 -> record 1
level 1 front 2 -> record 2
level 2 front 4 -> record 4
level 3 front 7 -> record 7
queue empty
answer 7
note level 2's leftmost is 4, even though 5 and 6 are also
on it -- and level 3 overwrites the answer entirelyComplexity
- Time — O(n): every node enqueued and dequeued once.
- Space — O(w): the queue holds at most one level.
Common Pitfalls
- Returning the leftmost node overall rather than the leftmost on the deepest level. Those differ whenever the deepest level does not descend from the leftmost branch.
- Recording the front after the level has been consumed, by which point the queue holds the next level.
- In the DFS variant, using
>=when comparing depths, which overwrites the leftmost with a later node at the same depth. - Mixing levels by reading the queue length inside the inner loop.
Full Solution — all languages
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution:
def findBottomLeftValue(self, root):
from collections import deque
q = deque([root]); res = root.val
while q:
res = q[0].val
for _ in range(len(q)):
n = q.popleft()
if n.left: q.append(n.left)
if n.right: q.append(n.right)
return resimport java.util.*;
class TreeNode {
int val;
TreeNode left, right;
TreeNode(int v) {
val = v;
}
}
class Solution {
public int findBottomLeftValue(TreeNode root) {
Queue<TreeNode> q = new LinkedList<>();
q.add(root);
int res = root.val;
while (!q.isEmpty()) {
int sz = q.size();
res = q.peek().val;
for (int i = 0; i < sz; i++) {
TreeNode n = q.poll();
if (n.left != null) q.add(n.left);
if (n.right != null) q.add(n.right);
}
}
return res;
}
}#include <bits/stdc++.h>
using namespace std;
struct TreeNode {
int val;
TreeNode *left, *right;
TreeNode(int v): val(v), left(nullptr), right(nullptr) {
}
};
class Solution {
public: int findBottomLeftValue(TreeNode* root) {
queue<TreeNode*> q;
q.push(root);
int res = root->val;
while (!q.empty()) {
int sz = q.size();
res = q.front()->val;
for (int i = 0; i < sz; i++) {
TreeNode* n = q.front();
q.pop();
if (n->left) q.push(n->left);
if (n->right) q.push(n->right);
}
}
return res;
}
};/**
* Tree node shape: { val, left, right }
* @param {TreeNode} root
* @return {number}
*/ var findBottomLeftValue = function(root) {
let q = [root], res = root.val;
while (q.length) {
res = q[0].val;
const nq = [];
for (const n of q) {
if (n.left) nq.push(n.left);
if (n.right) nq.push(n.right);
}
q = nq;
}
return res;
};