给定一棵二叉树,想象自己站在它的右侧,返回从顶部到底部看到的节点值。
例如:
给定以下二叉树,
   1            <---
 /   \
2     3         <---
 \     \
  5     4       <---
你应该返回 [1, 3, 4]。

详见:https://leetcode.com/problems/binary-tree-right-side-view/description/

Java实现:

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<Integer> rightSideView(TreeNode root) {
List<Integer> res=new ArrayList<Integer>();
if(root==null){
return res;
}
LinkedList<TreeNode> que=new LinkedList<TreeNode>();
que.offer(root);
int node=1;
while(!que.isEmpty()){
root=que.poll();
--node;
if(root.left!=null){
que.offer(root.left);
}
if(root.right!=null){
que.offer(root.right);
}
if(node==0){
res.add(root.val);
node=que.size();
}
}
return res;
}
}

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. Leetcode199. Binary Tree Right Side View二叉树的右视图

    给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值. 示例: 输入: [1,2,3,null,5,null,4] 输出: [1, 3, 4] 解释: 先求深度,中 ...

  3. [LeetCode] Binary Tree Right Side View 二叉树的右侧视图

    Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...

  4. [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 ...

  5. [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. 【LeetCode】199. Binary Tree Right Side View 解题报告(Python)

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

  7. leetcode 199 :Binary Tree Right Side View

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

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

  9. 【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, ...

随机推荐

  1. Oracle APEX 4.2安装和配置

    A standard Oracle 11.2.0.3 database installation comes bundled with Application Express (APEX) 3.2.1 ...

  2. ZXing-core生成二维码和解析

    如今二维码这么流行的时刻.也必须知道二维码是怎么生成.如今我们就来看看,是怎么生成的. 事实上主要是利用goggle公布的jar来使用:本文转自点击打开链接 1.二维码的生成 将Zxing-core. ...

  3. 李洪强iOS开发之RunLoop的原理和核心机制

    李洪强iOS开发之RunLoop的原理和核心机制 搞iOS之后一直没有深入研究过RunLoop,非常的惭愧.刚好前一阵子负责性能优化项目,需要利用RunLoop做性能优化和性能检测,趁着这个机会深入研 ...

  4. Ubuntu下配置Tomcat以指定(非root)身份执行

    My Blog:http://www.outflush.com/ 通常情况下.在配置Tomcat生产环境时,一般会配置Tomcat以特定的身份执行(非root).这样有利于提高安全性,防止站点被黑后的 ...

  5. Linux文档,目录命令

    1,Linux文件系统结构 Linux目录结构的组织形式和Windows有很大的不同,首先Linux没有"盘(C盘,D盘,E盘的概念)"的概念,已经建立文件系统的硬盘分区被挂载到某 ...

  6. CRM 配置 ADFS后,使用自定义STS遇到的问题总结

    1 登录ADFS服务查看 ADFS日志 2 根据日志提示的错误,设置ADFS对应的属性 (Get-ADFSRelyingPartyTrust) | Set-ADFSRelyingPartyTrust  ...

  7. [10.27_P2] 统计损失 (简单树形DP)

    树形DP 简单题 Description 给定一棵树,每个节点有一个值.对于一条路径,它的值为路径上所有点的值的乘积.求出树上所有路径的值的和. 注意:单个点也算一条路径. Input 第 1 行一个 ...

  8. AutoIT: 捕捉路由器登陆的小例子

    $url2 = "AX.XXX.XXX.COM" $oIE = _IECreate ($url2, , , , ) Sleep() ControlSetText('[REGEXPT ...

  9. NPU 2015年陕西省程序设计竞赛网络预赛(正式赛)F题 和谐的比赛(递推 ||卡特兰数(转化成01字符串))

    Description 今天西工大举办了一场比赛总共有m+n人,但是有m人比较懒没带电脑,另外的n个人带了电脑.不幸的是,今天机房的电脑全坏了只能用带的电脑,一台电脑最多两人公用,确保n>=m. ...

  10. 虚拟机C盘扩容

    使用 <分区助手> 下载地址:http://115.com/file/belj8wkm