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. Open Live writer 远程博客管理客户端

    1.  官网地址:http://openlivewriter.org/ 点击download下载:https://openlivewriter.azureedge.net/stable/Release ...

  2. 如何决定Web应用的线程池大小

    线程池(Thread Pool)在Web应用中线程池的大小决定了在任何一个时间点应用可以处理请求的并发数.如果一个系统收到的请求数超过了线程池的大小,那么超出的请求要么进入等待队列要么被拒绝.请注意, ...

  3. python文件操作与字符编码

    知识内容: 1.文件对象与文件处理流程 2.基本操作 3.上下文管理 4.文件的修改与文件内光标的移动 5.字符编码 一.文件对象与文件处理流程 1.文件对象 (1)文件分类 按文件中数据的组织形式可 ...

  4. HTML5 Canvas ( 线段端点的样式 ) lineCap

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  5. 奇技淫巧:在spring官网上下载历史版本的spring插件,springsource-tool-suite

    转自:https://blog.csdn.net/PacosonSWJTU/article/details/80959689 目前spring官网(http://spring.io/tools/sts ...

  6. require.js 学习基础

    RequireJS 是一个JavaScript模块加载器,他的目标是鼓励代码的模块化,它使用了不同于传统<script>标签的脚本加载步骤.可以用它来加速.优化代码,但其主要目的还是为了代 ...

  7. java web 读取文件,文件路劲不对的问题

    都知道,一般java项目,编译后的文件是在classes文件夹下面: 而java web项目,则是在WEB-INF/classes文件夹下面.new File(fileName)须先获取tomcat中 ...

  8. java自定义线程池

    如果并发的线程数量很多,并且每个线程都是执行一个时间很短的任务就结束了,这样频繁创建线程就会大大降低系统的效率,因为频繁创建线程和销毁线程需要时间.那么有没有一种办法使得线程可以复用,就是执行完一个任 ...

  9. as3 TweenMax TweenLite方法

    as3 TweenMax TweenLite方法补充(暂停.重新播放.倒序播放).现在来好好的学习一下:   TweenLite.to(mc, 1.5, {x:100}); 里面的mc指所作用的对象, ...

  10. Python之类属性的增删改查

    #类属性又称为静态变量,或者是静态数据,这些数据是他们所属的类对象绑定的,不依赖于任何类实例 class ChinesePeople: country = 'china' def __init__(s ...