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. [UE4]安卓打包成一个apk

    勾上就可以了

  2. 防火墙没有关导致外部访问虚拟机的tomcat遇到的问题和解决方法

    部署好tomcat,想在自己电脑上的浏览器访问,但是发现访问不了 访问方式是浏览器地址栏输入ip加端口,我的是192.138.211.121:8080,显示结果是无连接 在电脑上ping一下主机发现是 ...

  3. ExtJS 动态组件与组件封装

      介绍几个有用的函数: Ext.apply---追加配置选项Ext.reg,----注册xtypeExt.extend--扩展组件||操作({}|| cfg)fireEvent自定义事件机制 --- ...

  4. SQL中去掉字符串中最后一个字符(小技巧)

    --长度减一就可以了 select left(字段名,len(字段名)-1) from 表名

  5. uva-10112-计算几何

    题意:给你一些点,求这些点组成的三角形面积最大,而且三角形内不能包含其他点 #include <iostream> #include <math.h> #include< ...

  6. HAproxy使用

    参考官网 安装HAproxy/ pull 官方镜像 本地安装:本地安装路径:/usr/local/haproxy/配置: 添加:/usr/local/haproxy/conf/haproxy.cfg添 ...

  7. linux双网卡绑定实现冗余与负载均衡

    1 编辑/etc/modprobe.conf   在/etc/modprobe.conf里加入如下两行: alias bond0 bonding options bond0 mode=1 miimon ...

  8. 关于有些邮件可以在http上发送成功但是https不能发送成功一个思路方法

    关于有些邮件可以在http上发送成功但是https不能发送成功 其实如果是单纯的发送邮件,是没问题 今天一个客户出现这个问题,进行排查 他的邮件发送是任务制的, 是通过CURL请求的, 我估计她的CU ...

  9. HP服务器安装配置教程

    使用iLO远程管理HP系列服务器 http://blog.51cto.com/wangchunhai/837529

  10. net 编译报错:编辑器或项目正在尝试签出在内存中修改的文件,这将导致保存该文件

    1,报错提示: 编辑器或项目正在尝试签出在内存中修改的文件,这将导致保存该文件. 在生成过程中保存文件是危险的,这可能会在将来导致不正确的生成输出. 是否仍然继续签出? 2,原因:licenses.l ...