[LeetCode] 513. Find Bottom Left Tree Value_ Medium tag: BFS
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.
这个题的思路其实跟[LeetCode] 199. Binary Tree Right Side View_ Medium tag: BFS, Amazon里面我提到的left side view一样的思路, 只是返回的时候返回最后一个元素即可.
1. Constraints
1) root cannot be None, 所以edge case就是 1
2, Ideas
BFS: T: O(n), S: O(n) n is the number of the nodes of the tree
3. Code
class Solution:
def LeftViewMost(self, root):
ans, queue = [], collections.deque([(root, 0)])
while queue:
node, heig = queue.popleft()
if heig == len(ans):
ans.append(node.val)
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
return ans[-1]
2)
class Solution(object):
def findBottomLeftValue(self, root):
"""
:type root: TreeNode
:rtype: int
"""
ans, queue = (root.val, 0), collections.deque([(root, 0)])
while queue:
node, height = queue.popleft()
if height > ans[1]:
ans = (node.val, height)
if node.left:
queue.append((node.left, height + 1))
if node.right:
queue.append((node.right, height + 1))
return ans[0]
4. Test case
1) root is 1
2)
2
/ \
1 3 3)
1
/ \
2 3
/ / \
4 5 6
/
7
[LeetCode] 513. Find Bottom Left Tree Value_ Medium tag: BFS的更多相关文章
- LN : leetcode 513 Find Bottom Left Tree Value
lc 513 Find Bottom Left Tree Value 513 Find Bottom Left Tree Value Given a binary tree, find the lef ...
- [LeetCode] 261. Graph Valid Tree _ Medium tag: BFS
Given n nodes labeled from 0 to n-1 and a list of undirected edges (each edge is a pair of nodes), w ...
- 513. Find Bottom Left Tree Value - LeetCode
Question 513. Find Bottom Left Tree Value Solution 题目大意: 给一个二叉树,求最底层,最左侧节点的值 思路: 按层遍历二叉树,每一层第一个被访问的节 ...
- [LeetCode] 103. Binary Tree Zigzag Level Order Traversal _ Medium tag: BFS
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...
- 【LeetCode】513. Find Bottom Left Tree Value 解题报告(Python & C++ & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS DFS Date 题目地址:https:// ...
- 【leetcode】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 ...
- [LeetCode] 199. Binary Tree Right Side View_ Medium tag: BFS, Amazon
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...
- 513 Find Bottom Left Tree Value 找树左下角的值
给定一个二叉树,在树的最后一行找到最左边的值. 详见:https://leetcode.com/problems/find-bottom-left-tree-value/description/ C+ ...
- [LC] 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 ...
随机推荐
- python tkinter Listbox用法
python tkinter组件的Listbox的用法,见下面代码的演示: from tkinter import * root=Tk() v=StringVar() #Listbox与变量绑定' l ...
- MyBatis学习之输入输出类型
1. 传递pojo对象 Mybatis使用ognl表达式解析对象字段的值,#{}或者${}括号中的值为pojo属性名称,其中,#{}:占位符号,好处防止sql注入,${}:sql拼接符号, 简要说明 ...
- sencha touch 视图(view) activate与deactivate事件探讨
在sencha touch2.2中采用card布局 之前的需求是考虑show,hide事件发现不可取 http://www.cnblogs.com/mlzs/archive/2013/06/13/31 ...
- [转]13 Hours: The Secret Soldiers of Benghazi
转:http://www.imfdb.org/wiki/13_Hours:_The_Secret_Soldiers_of_Benghazi The following weapons were use ...
- Android 使用MediaPlayer 播放 视频
http://pan.baidu.com/s/1lgKLS package cn.bgxt.surfaceviewdemo; import java.io.File; import android.m ...
- Unity3D 面试ABC
最先执行的方法是: 1.(激活时的初始化代码)Awake,2.Start.3.Update[FixUpdate.LateUpdate].4.(渲染模块)OnGUI.5.再向后,就是卸载模块(TearD ...
- numpy中的reshape中参数为-1
上篇文章中的reshape(-1,2),有的时候不明白为什么会有参数-1,可以通过查找文档中的reshape()去理解这个问题 根据Numpy文档(https://docs.scipy.org/doc ...
- linux下面安装coreseek与mmseg
1tar xzvf coreseek-3.2.14.tar.gz2cd mmseg-3.2.14/./configure --prefix=/usr/local/mmseg3 checking for ...
- java8新特性之Optional类
NullPointException可以说是所有java程序员都遇到过的一个异常,虽然java从设计之初就力图让程序员脱离指针的苦海,但是指针确实是实际存在的,而java设计者也只能是让指针在java ...
- 安装支持eigen线性迭代的ceres_solver
Ceres可以求解以下形式的有界约束非线性最小二乘问题: 这种形式的问题来源于科学工程的多个领域,从统计学的曲线拟合到计算机视觉中从图像中构建三维模型. 最近在做sfm方面的重建问题,需要对得到的相机 ...