题目:

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].

思路:

层次遍历法。遍历到每层最后一个节点时,把其放到结果集中。

/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {number[]}
*/
var rightSideView = function(root) {
var res=[];
if(root==null){
return res;
} var queue=[];
queue.push(root); while(queue.length!=0){
for(var i=0,len=queue.length;i<len;i++){
var cur=queue.pop();
if(cur.right){
queue.push(cur.right);
}
if(cur.left){
queue.push(cur.left);
}
}
res.push(cur.val);
} return res;
};

【树】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

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

  4. Leetcode之深度优先搜索(DFS)专题-199. 二叉树的右视图(Binary Tree Right Side View)

    Leetcode之深度优先搜索(DFS)专题-199. 二叉树的右视图(Binary Tree Right Side View) 深度优先搜索的解题详细介绍,点击 给定一棵二叉树,想象自己站在它的右侧 ...

  5. LeetCode 199. 二叉树的右视图(Binary Tree Right Side View)

    199. 二叉树的右视图 199. Binary Tree Right Side View 题目描述 给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值. Giv ...

  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

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

  8. 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. 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 ...

随机推荐

  1. CentOS7中实用的命令总结

    一:软件安装配置方面 这里总结一下对个人很实用的功能 1.查询rpm包的依赖哪些文件:rpm -qpR xx.rpm,然后rpm会分析此包的文件依赖性 2.查询系统中安装了哪些rpm包:rpm -qa ...

  2. linux (centos 6.4)下编译安装git

    是时候动手尝试下 Git 了,不过得先安装好它.有许多种安装方式,主要分为两种,一种是通过编译源代码来安装:另一种是使用为特定平台预编译好的安装包(yum install git). 若是条件允许,从 ...

  3. 一次简单完整的自动化登录测试-基于python+selenium进行cnblog的自动化登录测试

    Web登录测试是很常见的测试,手动测试大家再熟悉不过了,那如何进行自动化登录测试呢!本文就基于python+selenium结合unittest单元测试框架来进行一次简单但比较完整的cnblog自动化 ...

  4. 【PAT Advanced Level】1014. Waiting in Line (30)

    简单模拟题,注意读懂题意就行 #include <iostream> #include <queue> using namespace std; #define CUSTOME ...

  5. Oracle SQL Trace 和 10046 事件

    http://blog.csdn.net/tianlesoftware/article/details/5857023 一. SQL_TRACE 当SQL语句出现性能问题时,我们可以用SQL_TRAC ...

  6. linux系统编程之管道(二):管道读写规则

    一,管道读写规则 当没有数据可读时 O_NONBLOCK disable:read调用阻塞,即进程暂停执行,一直等到有数据来到为止. O_NONBLOCK enable:read调用返回-1,errn ...

  7. SQL SERVER 2014--内存表实现秒杀场景

    ===================================== 网上针对“秒杀”的解决方案很多,数据拆分化解热点,READPAST解决锁问题,应用程序排队限制并发等等很多方式,各有优缺点, ...

  8. mongodb 命令行安装

    因为下载zip的文件速度快,所以就使用了zip,zip格式的解压完后需要使用命令行安装,步骤大致如下: 1,首先创建一个文件叫mongo的文件,里面包含了数据库存放的目录以及日志,然后在指定的目录下创 ...

  9. C#: 获取当前路径不要用Environment.CurrentDirectory

    网上大把文章写到C#获取当前路径的方法如下: // 获取程序的基目录. System.AppDomain.CurrentDomain.BaseDirectory // 获取模块的完整路径. Syste ...

  10. H5实现页面内跳转页面

    <!DOCTYPE html><html><body> <iframe src="/example/html/demo_iframe.html&qu ...