Find Bottom Left Tree Value

Medium

Topics
TreeBFSDFS

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

Example 1

123
The answer is the leftmost value on the lowest level — which is not necessarily a left child, just the first node encountered on that level. A level-order walk that records the first node of each level gives it directly: 1.
Tree given in level-order (breadth-first); null marks a missing child.
Input:  root = [2,1,3]
Output: 1

Example 2

4217536
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

  1. Queue with the root; remember the front value at the start of each level.
  2. Process exactly one level per outer iteration.
  3. 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 entirely

Complexity

  • 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 res
SensAI
Beta
Tap the mic to stop voice mode