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. 使用Selectivizr让你的 CSS3选择器 通吃IE6/7/8

    说到HTML5,总是会让人不自觉的想到CSS3,貌似他们就应该是成双成对.OK!前几天和大家分享了<使用html5shiv让HTML5通吃IE6/7/8>,那今天,便再和大家分享一个能让H ...

  2. 消耗资源的SQL的定位方法;

    解答:select sql_text from v$sql where disk_reads > 1000 or (executions > 0 and buffer_gets/execu ...

  3. pip使用代理下载

    sudo pip install <packageName>的时候有时候会遇到connection error,原因是sudo的环境变量没有继承普通用户的环境变量,这样会导致普通用户设置的 ...

  4. 关于OBJC

    http://www.objc.io/ objc这个站点是:关于objective-c语言的最佳实践和高阶技术的期刊. 看了几期非常不错,所以计划每天抽出时间翻译一篇文章和大家一起分享.

  5. hbase0.96.0单机模式安装(win7 无需cygwin)

        之前折腾了几天,想让hbase的单机模式在cygwin上跑起来,都不成功.正当我气馁之时,我无意中发现hbase0.96.0的bin和conf目录下有一些扩展名为cmd的文件.这难道是给win ...

  6. ChemDraw在苹果电脑上能不能用

    很多ChemDraw 15.1 Pro用户都Windows操作系统用户,近年来随着苹果公司的影响力越来越大,使用苹果电脑的朋友越来越多.一些ChemDraw用户可能会使用苹果电脑,因此特别的关注在苹果 ...

  7. IPOL图像处理分析经典在线(文献+源码)

    网址: IPOL Journal · Image Processing On Line https://www.ipol.im/ 分类: 搜索: 下载文献和源码: NLM算法:IPOL Journal ...

  8. mysql core文件的正确打开姿势

         最近两天自己负责的一个实例频繁出现crash的情况,分析了日志,大致明白了crash的原因,但是没有定位到具体的SQL,也没有找到很好的规避的办法,因此想在mysql出现crash的时候自动 ...

  9. 【BZOJ3039】玉蟾宫 单调栈

    [BZOJ3039]玉蟾宫 Description 有一天,小猫rainbow和freda来到了湘西张家界的天门山玉蟾宫,玉蟾宫宫主蓝兔盛情地款待了它们,并赐予它们一片土地.这片土地被分成N*M个格子 ...

  10. PAT 1018 Public Bike Management(Dijkstra 最短路)

    1018. Public Bike Management (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...