[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 nodes you can see ordered from top to bottom.
Example:
Input: [1,2,3,null,5,null,4]
Output: [1, 3, 4]
Explanation: 1 <---
/ \
2 3 <---
\ \
5 4 <---
这个题目也是经典的BFS, 依次scan tree的每一层, 然后将第一个看到的元素append进入ans里面即可. 这里思路是根据ans的size来判断该层是否已经append过了, 如果append过了那么就继续测试他的children. 最后返回ans.
1. Constraints
1) Tree 可以为empty, 但是因为返回的是array, 所以可以将edge case放到queue里面去判断即可.
2. Ideas
BFS T: O(n), S: O(n) n is number of nodes of the tree
1) ans(init: []), queue( init: [root, 0])
2) while queue: node, heig = queue.popleft(), 根据len(ans), 和heig的大小来判断该层是否被append过, 如果没有, 那么append进入ans
3) 先判断right children, 因为是right view, 那么同理如果出一个题目, 问的是left view, 那么code基本不变, 只是将判断right children 和left children的顺序换下即可.
4) return ans
3. code
class Solution:
def rightSideView(self, root):
ans, queue = [], collections.deque([(root, 0)])
while queue:
node, heig = queue.popleft()
if node:
if heig == len(ans):
ans.append(node.val)
if node.right:
queue.append((node.right, heig + 1))
if node.left:
queue.append((node.left, heig + 1))
return ans
similar, if we change the question into Binary Tree Left Side View. then the code is similar, but change the order to append left and right child.
class Solution:
def leftSideView(self, root):
ans, queue = [], collections.deque([(root, 0)])
while queue:
node, heig = queue.popleft()
if node:
if heig == len(ans):
ans.append(node.val)
if node.left:
queue.append((node.left, heig + 1))
if node.right:
queue.append((node.right, heig + 1))
return ans
4. Test cases
1) empty tree
2)
1 <---
/ \
2 3 <---
\
5 <---
3)
1 <---
/ \
2 3 <---
\ \
5 4 <---
[LeetCode] 199. Binary Tree Right Side View_ Medium tag: BFS, Amazon的更多相关文章
- leetcode 199 :Binary Tree Right Side View
// 我的代码 package Leetcode; /** * 199. Binary Tree Right Side View * address: https://leetcode.com/pro ...
- leetcode 199. Binary Tree Right Side View 、leetcode 116. Populating Next Right Pointers in Each Node 、117. Populating Next Right Pointers in Each Node II
leetcode 199. Binary Tree Right Side View 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...
- [LeetCode] 199. Binary Tree Right Side View 二叉树的右侧视图
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...
- leetcode@ [199] Binary Tree Right Side View (DFS/BFS)
https://leetcode.com/problems/binary-tree-right-side-view/ Given a binary tree, imagine yourself sta ...
- 【leetcode】Binary Tree Maximum Path Sum (medium)
Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...
- Java for LeetCode 199 Binary Tree Right Side View
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...
- (二叉树 bfs) leetcode 199. Binary Tree Right Side View
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...
- [leetcode]199. Binary Tree Right Side View二叉树右视图
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...
- [leetcode]199. Binary Tree Right Side View二叉树右侧视角
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...
随机推荐
- 深入学习Make命令和Makefile(上)
https://www.zybuluo.com/lishuhuakai/note/209302 深入学习Make命令和Makefile(上) make是Linux下的一款程序自动维护工具,配合make ...
- laravel部署常用命令
php composer install composer dump-autoload php artisan key:generate .env 及 config/database.php里的数据库 ...
- rman 中遇到 ORA-01861
RMAN> run{ 2> sql 'alter session set nls_date_format="yyyy-mm-dd hh24:mi:ss"'; 3> ...
- LeetCode 31 Next Permutation(下一个全排列)
题目链接: https://leetcode.com/problems/next-permutation/?tab=Description Problem :寻找给定int数组的下一个全排列(要求 ...
- Json.NET Deserialize时如何忽略$id等特殊属性
由于$id.$ref等是默认Json.NET的特殊属性,在反序列化时不会将其对应的值填充,例如: [DataContract] public class MyObject { [DataMember( ...
- iOS教程:Core Data数据持久性存储基础教程
目录[-] 创建Core Data工程 创建数据模型 测试我们的数据模型 来看看SQL语句的真面目 自动生成的模型文件 创建一个表视图 之后看些什么? 就像我一直说的,Core Data是iOS编程, ...
- oracle fm格式化
select to_char(0.56,'FM999,999,990.00' ) from dual 其中 9代表如果存在数字则显示数字,不存在显示空格 其中 0代表如果存在数字则显示数字,不存在则显 ...
- linux启动程序和关闭程序脚本
关闭脚本: #!/bin/bash source /etc/profile log() { echo `date +[%Y-%m-%d" "%H:%M:%S]` $1 } log ...
- 思科SVI接口和路由接口区别
Cisco多层交换中提到了一个SVI接口,路由接口.在多层交换机上可以将端口配置成不同类型的接口. 其中SVI接口 类似于 interface Vlan10ip address 192.168.20 ...
- yii---控制器的创建
示例:在 controlls/ 路径新建 IndexController.php 控制器 类名要有 Controller 后缀 继承 yii\web\Controller <?php names ...