392/670

392. Find Bottom Left Tree Value

Medium

You are given the root of a binary tree with at least one node.

Look at the tree's deepest level (the row farthest from the root) and return the value of that row's leftmost node.

Careful: "leftmost node of the deepest row" is not necessarily a left child — a right child can be the only thing on the bottom row. The value itself is what you return, not a node or an index.

Example 1 — deepest row, leftmost nodeIn the tree [2, 1, 3] the deepest level is the pair {1, 3}; its leftmost member is 1.
213bottom-left = 1depth 0depth 1

Example 1:

Input: root = [2, 1, 3]

Output: 1

Explanation: The deepest level contains 1 and 3; the leftmost of the two is 1.

Example 2:

Input: root = [1, 2, 3, 4, null, 5, 6, null, null, 7]

Output: 7

Explanation: The levels are [1], [2, 3], [4, 5, 6], [7]. The deepest level holds only the node 7, so 7 is its leftmost value.

Constraints:

  • 1 ≤ number of nodes ≤ 10⁴
  • -2³¹ ≤ Node.val ≤ 2³¹ - 1

Hints:

Breadth-first search visits the tree level by level, so the answer is simply the first node of the last level you ever produce. Collect each level and remember its front element.

One queue trick removes all the bookkeeping: enqueue the right child before the left child. Then the very last node the BFS dequeues is the deepest level's leftmost node — return whatever you dequeued last.

▶ Run checks these sample cases. Submit also runs hidden edge cases.

Input: root = [2, 1, 3]

Expected output: 1