Skip to content

Latest commit

 

History

History
95 lines (53 loc) · 1.21 KB

0513._Find_Bottom_Left_Tree_Value.md

File metadata and controls

95 lines (53 loc) · 1.21 KB

513. Find Bottom Left Tree Value

难度: Medium

刷题内容

原题连接

内容描述

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.

解题方案

思路 1 - 时间复杂度: O(N)- 空间复杂度: O(N)******

class Solution:
    def findBottomLeftValue(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        if not root:
            return []
        res, cur_level = [], [root]
        while cur_level:
            next_level, tmp_res = [], []
            for node in cur_level:
                tmp_res.append(node.val)
                if node.left:
                    next_level.append(node.left)
                if node.right:
                    next_level.append(node.right)
            res.append(tmp_res)
            cur_level = next_level
        return res[-1][0]