【树】Binary Tree Right Side View
题目:
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的更多相关文章
- 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 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...
- leetcode 199 :Binary Tree Right Side View
// 我的代码 package Leetcode; /** * 199. Binary Tree Right Side View * address: https://leetcode.com/pro ...
- 【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, ...
- Leetcode之深度优先搜索(DFS)专题-199. 二叉树的右视图(Binary Tree Right Side View)
Leetcode之深度优先搜索(DFS)专题-199. 二叉树的右视图(Binary Tree Right Side View) 深度优先搜索的解题详细介绍,点击 给定一棵二叉树,想象自己站在它的右侧 ...
- LeetCode 199. 二叉树的右视图(Binary Tree Right Side View)
199. 二叉树的右视图 199. Binary Tree Right Side View 题目描述 给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值. Giv ...
- 【LeetCode】199. Binary Tree Right Side View 解题报告(Python)
[LeetCode]199. Binary Tree Right Side View 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/probl ...
- 【刷题-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, ...
- Binary Tree Right Side View
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...
- 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 ...
随机推荐
- 有关在 Word 中撰写博客的帮助
目前大部分的博客作者在用Word写博客这件事情上都会遇到以下3个痛点: 1.所有博客平台关闭了文档发布接口,用户无法使用Word,Windows Live Writer等工具来发布博客.使用Word写 ...
- osgearth
https://max.book118.com/html/2018/0521/167783983.shtm https://baike.1688.com/doc/view-d36134276.html
- Tensorflow currently has no official prebuild for your CUDA, cuDNN combination.
INFO CUDA version: 10. ERROR cuDNN not found. See https://github.com/deepfakes/faceswap/blob/master/ ...
- idea 优化
http://www.iyunv.com/thread-348537-1-1.html
- opencpu
前端通过它调用后端的R语言,对R函数进行一个封装. 网址:https://github.com/jeroenooms/opencpu.js 使用的是opencpu-0.5.js,对它进行了修改. 1. ...
- console的所有用法
http://jingyan.baidu.com/article/e75aca855c6419142edac6c1.html 参考它. console.info(), console.debug() ...
- oracle 游标实现多重循环
declare -- Local variables here i integer; cursor c_province is select ds.swjg_dm from dm_swjg ds wh ...
- Java内存模型以及Volatile、Synchronize关键字的疑问
1.众所周知,java的内存模型是一个主内存,每个线程都有一个工作内存空间,那么主内存同步到工作内存是什么时候发生的呢?工作内存同步会主内存又是什么时候发生的呢? 在cpu进行线程切换时就会发生这些同 ...
- LeetCode149:Max Points on a Line
题目: Given n points on a 2D plane, find the maximum number of points that lie on the same straight li ...
- linux系统编程之文件与IO(六):实现ls -l功能
本文利用以下系统调用实现ls -l命令的功能: 1,lstat:获得文件状态, 2,getpwuid: #include <pwd.h> struct passwd *getpwuid(u ...