[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 ...
随机推荐
- POP3命令与分析
http://www.cnblogs.com/crystalray/p/3302121.html POP3(Post Office Protocol 3)即邮局协议的第3个版本,它是规定个人计算机如何 ...
- android cannot locate symbol 'sigemptyset'问题解决
设备是android 4.1的平板电脑,支持armeabi-v7a和mips,为了能用上poco c++ lib,用cmake编译了poco mips架构的lib,但在android studio里引 ...
- LeetCode 43 Multiply Strings(字符串相乘)
题目链接: https://leetcode.com/problems/multiply-strings/?tab=Description 求解大数相乘问题 按照上图所示,进行嵌套循环计算 ...
- ThinkPHP-5.0.23新的RCE漏洞测试和POC
TP5新RCE漏洞 昨天又是周五,讨厌周五曝漏洞,还得又得加班,算了,还是先验证一波.新的TP5RCE,据说发现者因为上次的RCE,于是又审计了代码,结果发现的.TP5也成了万人轮啊. 测试 环境搭建 ...
- 不同.NET Framework版本下ASP.NET FormsAuthentication的兼容性
假设站点A加密使用.NET Framework 2.0,站点B解密使用.NET Framework 4.0,除了保持MachineKey相同外还需要进行如下设置: 1.Web.config的<a ...
- 解决启动Distributed Transaction Coordinator服务出错的问题
解决启动Distributed Transaction Coordinator服务出错的问题 "Windows 不能在 本地计算机 启动 Distributed Transaction ...
- Penn Treebank
NLP中常用的PTB语料库,全名Penn Treebank.Penn Treebank是一个项目的名称,项目目的是对语料进行标注,标注内容包括词性标注以及句法分析. 语料来源为:1989年华尔街日报语 ...
- 9.9Dajngo MTV
2018-9-9 14:53:53 mvc框架和 Django的MTV框架 框架参考 :https://www.cnblogs.com/liwenzhou/p/8296964.html 2018-9- ...
- 利用 background 和 filter 模糊指定区域
背景知识:background-size: cover;,background-attachment:fixed;,filter:blur() 难题: 通常,我们会通过filter:blur()去实现 ...
- PPTP客户端无法拨号778故障解决
Win7客户端无法拨号提示778 CentOS服务器端 日志/var/log/messages提示一下错误信息 GRE: read(fd=6,buffer=611860,len=8196) from ...