Skip to content

Latest commit

 

History

History
67 lines (43 loc) · 1.58 KB

0513-find-bottom-left-tree-value.adoc

File metadata and controls

67 lines (43 loc) · 1.58 KB

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.

思路分析

常规思路是广度优先,记录每一层第一个节点的值。

看社区讨论,可以在广度优先的基础上做个优化:通常在队列里面先放左节点,再放右节点。反转一下,先放右节点,再放左节点,那么最后一个节点就是需要的节点。

广度优先比较熟悉,尝试了一下深度优先的解法。

link:{sourcedir}/_0513_FindBottomLeftTreeValue.java[role=include]