513 Find Bottom Left Tree Value

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

Example 1:

Input:

    2
   / \
  1   3

Output:
1

Example 2:

Input:

        1
       / \
      2   3
     /   / \
    4   5   6
       /
      7

Output:
7

Note:You may assume the tree (i.e., the given root node) is not NULL.

Complexity: O(n) time, O(logn) space

The Idea: Run a level order traversal with a small twist: check for instances when the next level arises (left to right top to bottom), and collect the first left instance.

Last updated

Was this helpful?