AAlgoLoopSpaced repetition for LeetCode
MEDIUMBFS / DFSLeetCode ↗

Find Bottom Left Tree Value

The key idea

The answer is the first node you reach on the deepest level. A level-order (BFS) sweep that records the first node of every row leaves the last row's first node as the answer; a depth-first walk that goes left-first and only updates when it reaches a strictly deeper level lands on the same node.

Problem

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

The last row is the deepest level of the tree. If that level holds several nodes, return the value of the one furthest to the left.

Constraints

Examples

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

Complexity

Time: O(n) Space: O(n)

See the full solution

410310
Step-by-step visualization
Start free →

More BFS / DFS problems