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值
class Solution(object):
def findBottomLeftValue(self, root):
"""
:type root: TreeNode
:rtype: int
"""
left = 0
if(not root):
return left
result = []
queue = [root]
while(queue):
count = len(queue)
for i in range(0, count):
node = queue.pop(0)
if i == 0:
left = node.val
if(node.left):
queue.append(node.left)
if(node.right):
queue.append(node.right)
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