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

Posted Long Long Journey

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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





以上是关于513. Find Bottom Left Tree Value 二叉树左下节点的值的主要内容,如果未能解决你的问题,请参考以下文章

513. Find Bottom Left Tree Value

LeetCode - 513. Find Bottom Left Tree Value

513. Find Bottom Left Tree Value - LeetCode

leetcode 513. Find Bottom Left Tree Value

[LC] 513. Find Bottom Left Tree Value

[LeetCode]513 Find Bottom Left Tree Value(BFS)