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

思路

DFS

每当recursion一进入到next level,

就立马加上该level的right side node到result里

对应的,

在recursion的时候,先处理 root.right

代码

 class Solution {
     public List<Integer> rightSideView(TreeNode root) {
         List<Integer> result = new ArrayList<>();
         if(root == null) return result;
         dfs(root, result, );
         return result;
     }

     private void dfs(TreeNode root, List<Integer> result, int level ){
         // base case
         if(root == null) return;
         /*height == result.size() limits the amount of Node add to the result
         making sure that once go to the next level, add right side node to result immediately
         */
         if(level == result.size()){
             result.add(root.val);
         }
         // deal with right side first, making sure right side node to be added first
         dfs(root.right, result, level+);
         dfs(root.left, result, level+);
     }
 }

思路

BFS(iteration)

每次先将right side node 加入到queue里去

保证 当i = 0 的时候,poll出来的第一个item是right side node

代码

 public List<Integer> rightSideView(TreeNode root) {
         // level order traversal
         List<Integer> result = new ArrayList();
         Queue<TreeNode> queue = new LinkedList();
         // corner case
         if (root == null) return result;

         queue.offer(root);
         while (!queue.isEmpty()) {
             int size = queue.size();
             for (int i = 0; i< size; i++) {
                 TreeNode cur = queue.poll();
                 // make sure only add right side node
                 if (i == 0) result.add(cur.val);
                 // add right side node first, making sure poll out first
                 if (cur.right != null) queue.offer(cur.right);
                 if (cur.left != null) queue.offer(cur.left);
             }
         }
         return result;
     }

[leetcode]199. Binary Tree Right Side View二叉树右视图的更多相关文章

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

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

  3. leetcode 199 :Binary Tree Right Side View

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

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

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

  6. 199 Binary Tree Right Side View 二叉树的右视图

    给定一棵二叉树,想象自己站在它的右侧,返回从顶部到底部看到的节点值.例如:给定以下二叉树,   1            <--- /   \2     3         <--- \  ...

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

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

  9. 【LeetCode】199. Binary Tree Right Side View 解题报告(Python)

    [LeetCode]199. Binary Tree Right Side View 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/probl ...

随机推荐

  1. 2天时间终于把ntopng装好了

    1.环境centos6.7x642.安装步骤,首先把centos按优化步骤完成3.更改centos的yum源,更改为阿里云的源.4.[root@netmon ntopng]# cat /etc/yum ...

  2. ESXI5.5设置主机的时间自动同步服务 NTP

    背景:现在公司的很多线上服务也都通过虚拟化来实现,最近遇到一个小问题,虚拟机上的时间不准确.原来是虚拟机会主动同步宿主机时间,一般虚拟机中都安装vmware tool工具,这个工具会自动和宿主机进行时 ...

  3. thinkphp 5.0手记

    场景配置,可配置多个数据库,按需求加载 数组合并:array_merge();键名相同后面覆盖前面 array_merge_recursive();键名相同,键值合并 对与http://localho ...

  4. SpringMVC整合Hessian访问远程服务

    1.1     Hessian简介       Hessian是一个轻量级的Web服务实现工具,它采用的是二进制协议,因此很适合发送二进制数据.它的一个基本原理就是把远程服务对象以二进制的方式进行发送 ...

  5. 解决Sublime 3提示 Sublime Text Error while loading PyV8 binary

    转自:http://blog.initm.com/sublime-text/ 今天打开sublime遇到一个提示  如上图Sublime Text Error while loading PyV8 b ...

  6. 初步认识AutoMapper

      AutoMapper 初步认识AutoMapper 前言 手动映射 使用AutoMapper 创建映射 Conventions 映射到一个已存在的实例对象   前言 通常在一个应用程序中,我们开发 ...

  7. 深度学习RNN实现股票预测实战(附数据、代码)

    背景知识 最近再看一些量化交易相关的材料,偶然在网上看到了一个关于用RNN实现股票预测的文章,出于好奇心把文章中介绍的代码在本地跑了一遍,发现可以work.于是就花了两个晚上的时间学习了下代码,顺便把 ...

  8. nginx技术分享 (转)

    原文地址:http://blog.csdn.net/nethibernate/article/details/6628267 Nginx的作用: HTTP Server 反向代理,用于将用户的请求转发 ...

  9. python之路之函数02

    一  函数的参数: 我们把函数的参数分为形式参数和实际参数,简称形参和实参. 形参:在定义函数时,函数名括号内定义的参数. 实参:在调用函数时,函数名括号内需要用户传入的值. 注意: 实参值(相当于变 ...

  10. css常用字体

    宋体 SimSun 黑体 SimHei 微软雅黑 Microsoft YaHei 微软正黑体 Microsoft JhengHei 新宋体 NSimSun 新细明体 PMingLiU 细明体 Ming ...