题目:

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. 状态机中的RAM注意的问题--减少扇出的办法

    可能我不会抓紧时间,所以做事老是很慢.最近在整维特比译码过程深感自己有这样的毛病. 每天会有一点进展,但是却是一天的时间,感觉别人都做起事情来很快.可能这个东西有点难,做 不做得出来都不要紧,但我的想 ...

  2. cuDNN

    https://developer.nvidia.com/developer-program https://developer.nvidia.com/cudnn cuda和cuDNN的关系 http ...

  3. hdu 1425

    题目 这道题用快排做总是会超时,但是别人的快排就不会超时,最后看博客说最保险的方法还是用哈希的思想[哈希思想:散列再循环,对每一个数字进行通过改变哈希表的地址散列放置,将散列地址的哈希表记为1,这样 ...

  4. linux 通过md5查找重复文件

    代码如下: md5sum *|sort |uniq -w32 -D|awk -F ' ' '{print $2}' uniq 部分参数 -c #在每行前显示该行重复次数. -d #只输出重复的行. - ...

  5. Paul and Joyce are going to a movie(More listening of Unit 2)

      Paul: Hurry up, Joyce. We need to leave now if we're going to get to the theater a half hour befor ...

  6. Intellij IDEA 14的注册机(Java版)

    import java.math.BigInteger; import java.util.Date; import java.util.Random; import java.util.zip.CR ...

  7. Asp.net Core2.0, 基于 claims 实现权限验证

    https://www.cnblogs.com/KimmyLee/p/6430474.html

  8. navicat远程连接oracle

    本机没有oracle,这个软件太大了. 想要远程连接oracle,本地不安装oracle的话是不行的,我们安装一个oracle instance client,然后配置navicat就ok了. 下载i ...

  9. 在TFS中使用Git Tags(标签或标记),实现代码的版本管理

    一.概述: 与TFVC中标记(Label)一样,Git的标签(Tag)也是TFS系统的代码管理中非常重要的一个版本管理工具.使用标签,我们可以每个时间点的代码注上一个通俗.并且容易记忆的名称(例如标签 ...

  10. 曲演杂坛--使用ALTER TABLE修改字段类型的吐血教训

    --===================================================================== 事件起因:开发发现有表插入数据失败,查看后发现INT类型 ...