[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 ...
随机推荐
- read by other session 等待事件。
今天是2014-01-06,从今天开始,打算春节之前每天学习一个等待事件,今天就记录一下read by other session这个等待事件笔记. 什么是read by other session? ...
- iPhone X的缺口和CSS
苹果公司(Apple)的发布会也开完了,新产品也将登陆了.估计很多开发人员看到iPhone X的设备是要崩溃了,特别对于前端开发人员更是如此. iPhone X的屏幕覆盖了整个手机的屏幕,为相机和其他 ...
- Learning Git by Animations
https://hujiaweibujidao.github.io/blog/2016/05/20/learning-git-by-animations/ http://learngitbranchi ...
- Panel结构
参考weui的Panel结构 核心:定位,补充:对容器设置font-size:0,消除img下多出的3px,防止居中出现偏差. <!DOCTYPE html> <html lang= ...
- LeetCode 38 Count and Say(字符串规律输出)
题目链接:https://leetcode.com/problems/count-and-say/?tab=Description 1—>11—>21—>1211—>111 ...
- 部署OpenStack问题汇总(七)--解决apache启动错误"httpd:Could not reliably determine..."
今天在调试openstack的时候,重启apache,出现以下报错: [root@hctrl log]# service httpd restart 停止 httpd:[确定] 正在启动 httpd: ...
- windows中安装pip,setuptools,django等
1,安装Python3.6 (下载exe文件,双击安装) 注意设置环境变量,让Python的在任意位置都可以执行 .Python 下载地址:https://www.python.org/do ...
- [压缩]C#下使用SevenZipSharp压缩解压文本
using SevenZip; using System; using System.Collections.Generic; using System.IO; using System.Linq; ...
- mysql补充(4)数据完整性
数据完整性(Data Integrity)是指数据的精确性(Accuracy) 和可靠性(Reliability).(补充mysql数据完整性和约束) 它是应防止数据库中存在不符合语义规定的数据和防止 ...
- mysql rowid实现
) a, b b表是数据表