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.

求二叉树左下方节点的值,使用广度优先遍历,每遍历一层刷新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





posted @ 2017-08-13 22:28  xiejunzhao  阅读(215)  评论(0编辑  收藏  举报