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. ubuntu18.04 安装hadoop 2.7.3+hive 2.3.4

    1. 安装hadoop 详细请参见本人的另外一片博文<Hadoop 2.7.3 分布式集群安装> 2. 下载hive 2.3.4 解压文件到/opt/software -bin.tar.g ...

  2. BPM编程模型(场景)

    一直开发基于操作的业务系统,主要就是通过界面,用户提交一些数据完成任务,大多数涉及多人协作的,基本都是浏览,少数可能对其进行审批,这里的审批不是电子政务那样的多人审批任务,仅仅是对数据的一个操作而已, ...

  3. JavaScript中的坑

    内容:关于JavaScript中的一些蛋疼的问题以及面试笔试中常见的一些坑爹套路总结 此部分内容持续总结完善中... 1.undefined和null的区别 null: Null类型,代表空值,代表一 ...

  4. 柒月风华BBS上线

    论坛地址:https://3003soft.top/LBBS/ 欢迎大家加入. 开放式轻论坛:记录好玩.有趣的事儿:一起努力,一起前进: 希望能建立一个分享各类解决方案的社区

  5. 类unix系统同步目录,却不同步目录中文件

    rsync -av --del -f '+ */' -f '- *' src/ dst/;用此条命令即可同步同主机间不同目录到一个位置,或是同步道不同主机同位置. 或是用以下命令: ssh 10.18 ...

  6. python入门-存储数据

    这里的存储数据使用json格式 json   是javascript object notation的意思  javascript的对象标记 1 写入 json.dump import json nu ...

  7. 1.纯 CSS 创作一个按钮文字滑动特效 + 弹幕(残缺)

    原文地址:1# 视频演示如何用纯 CSS 创作一个按钮文字滑动特效 扩展后地址:https://scrimba.com/c/cJkzMfd HTML代码: <html> <head& ...

  8. php中点击链接直接下载图片

    最近需要一个功能,是点击链接,直接把图片下载下来,一般情况下,图片是在新页直接打开的,不会自动提示下载,在网上找来找,用这个挺好使,代码如下: $filename = basename($downfi ...

  9. 基于二进制RPC协议法的轻量级远程调用框架 ---- Hessian

    使用Java创建Hessian服务有四个步骤: 1.创建Java接口作为公共API                             (client和server端 创建一个相同的借口) 2.使 ...

  10. VBA 禁止保存

    禁止保存 在workbook事件中 Private Sub Workbook_BeforeClose(Cancel As Boolean)    Me.Saved = TrueEnd Sub Priv ...