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. C++ sort函数用法 C中的qsort

    需要包含#include <algorithm>MSDN中的定义: template<class RanIt>     void sort(RanIt first, RanIt ...

  2. iPhone X的缺口和CSS

    苹果公司(Apple)的发布会也开完了,新产品也将登陆了.估计很多开发人员看到iPhone X的设备是要崩溃了,特别对于前端开发人员更是如此. iPhone X的屏幕覆盖了整个手机的屏幕,为相机和其他 ...

  3. 流程图 --- BPMN规范简介

    BPMN 目前 是2.0规范 http://www.bpmn.org/   BPMN Quick Guide http://blog.csdn.net/flygoa/article/details/5 ...

  4. Internet Message Access Protocol --- IMAP协议

    https://en.wikipedia.org/wiki/Internet_Message_Access_Protocol   Internet Message Access Protocol

  5. sencha touch 在安卓中横屏、竖屏切换 应用崩溃问题

    答案来至于 Sencha Touch 交流 @周旭 这是由于横竖屏转换导致activity重跑onCreate方法导致的,有两种解决方案:1.横竖屏转换的时候不要重新跑onCreate方法,这个可以在 ...

  6. 让google.com不跳转到google.com.hk

    自从google的服务器搬离中国大陆后,大陆地区用户用google服务时会自动跳转到香港的http://google.com.hk,,有关键字过滤而且偶尔不是很稳定,这对我们的生活工作都造成了困扰. ...

  7. ConfluenceRemoteUserAuth

    配置confluence使用httpHeader的方式进行登录(目标版本:atlassian-confluence-6.3.3) 前提是已经安装好了Confluence,并且前端使用apache或者n ...

  8. layer.load()加载层如何加入文字描述

    https://fly.layui.com/jie/3586/ https://www.layui.com/doc/modules/layer.html#layer.load //loading层va ...

  9. R因子

    factor(x = character(), levels, labels = levels, exclude = NA, ordered = is.ordered(x), nmax = NA) l ...

  10. 转基于概率的矩阵分解原理详解(PMF)

    上一篇博客讲到了推荐系统中常用的矩阵分解方法,RegularizedMF是对BasicMF的优化,而PMF是在RegularizedMF的基础上,引入概率模型进一步优化.假设用户U和项目V的特征矩阵均 ...