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的更多相关文章

  1. leetcode 199 :Binary Tree Right Side View

    // 我的代码 package Leetcode; /** * 199. Binary Tree Right Side View * address: https://leetcode.com/pro ...

  2. 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 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...

  3. [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 ...

  4. 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 ...

  5. 【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. ...

  6. 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 ...

  7. (二叉树 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 ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. MFC onpaint() ondraw()

    OnPaint是WM_PAINT消息的消息处理函数,在OnPaint中调用OnDraw,一般来说,用户自己的绘图代码应放在OnDraw中. OnPaint()是CWnd的类成员,负责响应WM_PAIN ...

  2. 2. React组件的生命周期

    2. React组件的生命周期 使用React开发时候用到最多的就是React的组件了,通过继承React.Component,加入constructor构造函数,实现Render方法即可.这当中Re ...

  3. C#设计模式--工厂方法模式

    0.C#设计模式-简单工厂模式 设计模式: 工厂方法模式(Factory Method Pattern) 介绍:简单工厂模式是要在工厂类中通过数据来做个决策,在工厂类中的多个类中实例化出来其中一个要用 ...

  4. 题目1099:后缀子串排序(qsort函数自定义cmp函数)

    题目链接:http://ac.jobdu.com/problem.php?pid=1099 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...

  5. [原]Failed to load SELinux policy. System Freezing ----redhat7or CentOS7 bug

    重启rhel7或者centos7 启动界面按 e 在启动项后面加上enforcing=0 Ctrl+x  运行修改后的grub 进入系统 编辑保存/etc/selinux/config 重启

  6. backface-visibility 属性

    图片img加了backface-visibility 属性, 图片变清晰, 原因不明 a,img{ backface-visibility:hidden; -webkit-backface-visib ...

  7. 使用VLC推送TS流(纯图版)

    在没有编码器的情况下,可以使用VLC进行推送TS+UDP流 操作步骤如下: 一.UDP方式: 媒体-->流 选用要播放的文件,可以选择多个来播放,选择串流播放 这里直接点击下一步 需要选择在本地 ...

  8. Pexpect学习:

    pexecpt run用法:格式:run(command,timeout=-1,withexitstatus=False,events=None,extra_args=None,logfile=Non ...

  9. Xcode快速排错

    EXTENDS:http://blog.csdn.net/guo_hongjun1611/article/details/8063009 1,模拟器运行完全没问题,真机运行失败. 有时候我们在模拟器上 ...

  10. http后台json解析实例

    localhost:8080/hbinterface/orderInterface/sIReverseAccept.do?bizType=4&&bnetAccount=ESBTEST2 ...