[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 ...
随机推荐
- Building Boost for Android with error “cannot find -lrt”
编辑tools/build/src/tools/gcc.jam rule setup-threading ( targets * : sources * : properties * ){ local ...
- 题目1013:开门人和关门人(结构体自定义cmp排序)
题目链接:http://ac.jobdu.com/problem.php?pid=1013 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...
- java中调用操作系统的命令
java.lang.Runtime类提供了exec() 方法来执行操作系统的命令. 使用静态的Runtime.getRuntime()方法可以获得当前的java应用程序对应的Runtime类的实例 R ...
- 【JSP】JSP的介绍和基本原理
JSP简介 JSP的核心实质是Servlet技术.JSP是后来添加的基于Servlet的一种扩展技术.但二者在使用上有不同的方向. 由于Servlet实质是一个Java类,因此非常适合用来处理业务逻辑 ...
- AD添加LOGO的方法
1 将logo图片转换成单色的BMP 图像.简单的方法是使用Windows自带的画图程序,在将图片另存为时, 在文件类型下拉列表中选择单色.bmp即可.我们以Altium为例,如图所示将蓝色logo另 ...
- Centos 7.x系统安装后的初始化配置
1.配置IP.网关,编辑/etc/sysconfig/network-scripts/ifcfg-eno16777736 TYPE=Ethernet BOOTPROTO=none //默认为dhcp ...
- 自动释放池autoreleasepool
自动释放池是NSAutoreleasePool的实例,其中包含了收到autorelease消息的对象.当一个自动释放池自身被销毁(dealloc)时,它会给池中每一个对象发送一个release消息(如 ...
- Shell输入输出重定向
全部可用的重定向命令列表 命令 说明 command > file 将输出重定向到 file. command < file 将输入重定向到 file. command >> ...
- Hive show
CREATE TABLE page_view(viewTime INT, userid BIGINT,p_date timestamp, page_url STRING, referrer_url v ...
- 关于python爬虫的编码错误
现在才发现很多错误只有自己一点点的去尝试才能发现.不管之前别人怎么和你说,总是不可能面面俱到,所以提升自己的方法就是亲手实践,自己一点点的去发现问题,并一个个的解决.慢慢享受其中无言的快感. 今天就发 ...