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.

For example:
Given the following binary tree,

   1            <---
/ \
2 3 <---
\ \
5 4 <---

You should return [1, 3, 4].

其实就是层序遍历 的每层的最后一个元素!!!!

 class Solution {

     public List<Integer> rightSideView(TreeNode root) {
List<Integer> res = new ArrayList<Integer>();
Queue<TreeNode> queue = new LinkedList<TreeNode>();
if(root==null) return res;
queue.offer(root);
int level_num = 1;
while (!queue.isEmpty()) {
level_num = queue.size();
for(int i = 0; i < level_num; i++){
TreeNode node = queue.poll();
if(i==level_num-1)
res.add(node.val);
if(node.left != null) queue.offer(node.left);
if(node.right != null) queue.offer(node.right); }
}
return res;
}
}

199. Binary Tree Right Side View -----层序遍历的更多相关文章

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

  2. leetcode 199 :Binary Tree Right Side View

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

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

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

  4. 【刷题-LeetCode】199 Binary Tree Right Side View

    Binary Tree Right Side View Given a binary tree, imagine yourself standing on the right side of it, ...

  5. 【LeetCode】199. Binary Tree Right Side View

    Binary Tree Right Side View Given a binary tree, imagine yourself standing on the right side of it, ...

  6. [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. LeetCode之Binary Tree Level Order Traversal 层序遍历二叉树

    Binary Tree Level Order Traversal 题目描述: Given a binary tree, return the level order traversal of its ...

  8. LeetCode OJ 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. 102. Binary Tree Level Order Traversal ------层序遍历

      Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to righ ...

随机推荐

  1. SQLite 连接两个字符串

    SQLite中,连接字符串不是使用+,而是使用|| 示例: SELECT 'I''M '||'Chinese.' 将输出 I'M Chinese. 特别说明:1. SELECT 'I''M '+'Ch ...

  2. phpStorm格式化代码快捷键

    Ctrl+Alt+L    

  3. java . -- IDEA运行最简单的Java程序Hello World

    以下内容基于IDEA, JAVA EE已经安装好的情况. 如果没有安装好,网上的安装教程非常多的. 下面开始,18个图解 点击Finish 运行的代码如下: public class HelloWor ...

  4. js 跳转的几种方法收藏

    history.go(-n) 返回上一页(n 为返回前几页) window.location.reload(); 刷新当前页面 history.go(-1);window.locatoin.reloa ...

  5. java必备——经典的Hibernate

    在编程开发中,我们有非常多框架,他们有些非常方便,也非常有用,今天我们一起来认识一个java经典的框架Hibernate,Hibernate英文名称为"冬眠".这是个非常有意思的技 ...

  6. 为什么要使用docker

    1. 为什么要使用Docker Docker容器虚拟化的好处 Docker项目的发起人和Docker Inc.的CTO Solomon Hykes认为,Docker在正确的地点.正确的时间顺应了正确的 ...

  7. box-sizing与calc()与flex

    1,Syntax: /* Keyword values */ box-sizing: content-box; box-sizing: border-box; /* Global values */ ...

  8. noip2006 金明的预算

    题目链接:传送门 题目大意:略.. 题目思路:其实单就这道题来说,一个主件最多两个附件,且附件不再包含附件,所以很简单,但是如果主件的附件无限制,附件也可包含无限制的附件,应该怎么做? 首先推荐一篇论 ...

  9. async 的三大返回类型

    序 博主简单数了下自己发布过的异步文章,已经断断续续 8 篇了,这次我想以 async 的返回类型为例,单独谈谈. 异步方法具有三个可让开发人员选择的返回类型:Task<TResult>. ...

  10. 转SpringMVC Controller 返回值的可选类型

    spring mvc 支持如下的返回方式:ModelAndView, Model, ModelMap, Map,View, String, void. ModelAndView @RequestMap ...