码迷,mamicode.com
首页 > 其他好文 > 详细

513. Find Bottom Left Tree Value 二叉树左下节点的值

时间:2017-08-13 23:19:55      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:优先   遍历   lang   sel   广度   otto   last   print   bsp   

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.

求二叉树左下方节点的值,使用广度优先遍历,每遍历一层刷新result值

    1. class Solution(object):
    2. def findBottomLeftValue(self, root):
    3. """
    4. :type root: TreeNode
    5. :rtype: int
    6. """
    7. left = 0
    8. if(not root):
    9. return left
    10. result = []
    11. queue = [root]
    12. while(queue):
    13. count = len(queue)
    14. for i in range(0, count):
    15. node = queue.pop(0)
    16. if i == 0:
    17. left = node.val
    18. if(node.left):
    19. queue.append(node.left)
    20. if(node.right):
    21. queue.append(node.right)
    22. return left





513. Find Bottom Left Tree Value 二叉树左下节点的值

标签:优先   遍历   lang   sel   广度   otto   last   print   bsp   

原文地址:http://www.cnblogs.com/xiejunzhao/p/7355276.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!