[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 ...
随机推荐
- 剑指offer——49
丑数 因子只含2,3,5的数称为丑数. 怎么求第K大的丑数呢.K可以为10^7 最简单的做法是,对每个数判断是否为丑数. 复杂度为O( n * log(n) ),理论上是不行的. uglys[i] 来 ...
- Esper学习之七:EPL语法(三)
1.Aggregation 和SQL一样,EPL也有Aggregation,即聚合函数.语法如下: aggregate_function([all|distinct] expression) aggr ...
- SQL数据库对象名无效的解决方法
对象名 'dbo.xxxx' 无效. 最后找到如下方法解决:原因是必须把所有以前的所有者改为DBO就不会出问题了. 执行下面语句,更改所有表的所有者为DBO exec sp_msforeachtabl ...
- Word 2010 制作文档结构之图标自动编号设置
注意: 使用图片自动编号时,如果文档标题使用的样式是通过“将所选内容保存为新快速样式”所生成的样式,则图片自动编号不会生效 因此设置标题样式时,不要 新建样式,直接使用word预设的“标题 1”样式和 ...
- Excel中用countif和countifs统计符合条件的个数 good
countif单条件统计个数 1 就以下表为例,统计总分大于(包含等于)400的人数. 2 在J2单元格输入公式=COUNTIF(I2:I22,">=400") 3 回车 ...
- VScode首选项
首选项相关的设置: // 将设置放入此文件中以覆盖默认设置 { "workbench.iconTheme": "vscode-icons", "wor ...
- [原]openstack-kilo--issue(三) openstack-nova-issue-systemctl start openstack-nova-compute.service
本博客已经添加"打赏"功能,"打赏"位置位于右边栏红色框中,感谢您赞助的咖啡. execute systemctl start openstack-nova-c ...
- Openstack 在VMware虚拟机ESXI和Workstation下安装需要更改参数
[vmware vsphere] 要在esxi 5i的系统文件/etc/vmware/config最后添加vhv.allow = “TRUE” 一行.重启 VMware ESXi 后编辑虚拟机选项(需 ...
- 基于spring-cloud的微服务(3)eureka的客户端如何使用IP地址来进行注册
例子中和我写的代码里,使用的spring-boot的版本是2.0 Eureka的客户端默认是使用hostname来进行注册的,有的时候,hostname是不可靠的,需要使用IP地址来进行注册 name ...
- C程序设计语言习题(3-5)
编写函数itob(n,s,b),将整数n转换为以b为底的数,并将转换结果以字符的形式保存到字符串s中.e.g.itob(n,s,16)把整数n格式化为十六进制整数保存在s中. #include< ...