当前位置:网站首页>【leetcode】199.二叉树的右视图
【leetcode】199.二叉树的右视图
2022-04-23 10:27:00 【前端corner】
题目
给定一个二叉树的 根节点 root,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值。
示例 1:
输入: [1,2,3,null,5,null,4]
输出: [1,3,4]
示例 2:
输入: [1,null,3]
输出: [1,3]
示例 3:
输入: []
输出: []
提示:
二叉树的节点个数的范围是 [0,100]
-100 <= Node.val <= 100
思路

- 想一想,从右往左看,看到的节点都有哪些特点呢?每一层都能且只能看到一个节点,且这个节点是每一层的最后一个节点。
- 根据上面的分析,很显然用二叉树的层序遍历就行了当遇到每一层的最后一个节点时,就把该节点添加到结果数组即可。
代码

/** * Definition for a binary tree node. * function TreeNode(val, left, right) { * this.val = (val===undefined ? 0 : val) * this.left = (left===undefined ? null : left) * this.right = (right===undefined ? null : right) * } */
/** * @param {TreeNode} root * @return {number[]} */
var rightSideView = function(root) {
if(!root) return []
let queue = [root]
let res = []
while(queue.length){
const len = queue.length
for(let i = 0 ; i < len ; i++){
const curNode = queue.shift()
if(i === len - 1) res.push(curNode.val) //判断是否当层的最后一个节点,是的话就保存
if(curNode.left) queue.push(curNode.left)
if(curNode.right) queue.push(curNode.right)
}
}
return res
};
复杂度
- 时间复杂度: O ( n ) O(n) O(n)
- 空间复杂度: O ( n ) O(n) O(n)
关注我的专栏,每天更新三道leetcode题解,一起变强!
版权声明
本文为[前端corner]所创,转载请带上原文链接,感谢
https://blog.csdn.net/laplacepoisson/article/details/124359162
边栏推荐
- Examination questions and answers of the third batch (main person in charge) of Guangdong safety officer a certificate in 2022
- Solution architect's small bag - 5 types of architecture diagrams
- What about Jerry's stack overflow? [chapter]
- 454、四数之和(哈希表)
- MapReduce compression
- 利用多线程按顺序连续输出abc10次
- SQL Server 游标循环表数据
- Realize data value through streaming data integration (2)
- 一文看懂 LSTM(Long Short-Term Memory)
- 第三章 启用和调整IM列存储的大小(IM-3.1)
猜你喜欢

Windows installs redis and sets the redis service to start automatically

中职网络安全2022国赛之CVE-2019-0708漏洞利用

Examination questions and answers of the third batch (main person in charge) of Guangdong safety officer a certificate in 2022

Yarn core parameter configuration

微信小程序简介、发展史、小程序的优点、申请账号、开发工具、初识wxml文件和wxss文件

Juc并发编程06——深入剖析队列同步器AQS源码

Configuration of LNMP

Arm debugging (1): two methods to redirect printf to serial port in keil

mysql同一个表中相同数据怎么合并

SSH利用私钥无密钥连接服务器踩坑实录
随机推荐
Realizing data value through streaming data integration (4) - streaming data pipeline
Ansible playbook syntax and format automate cloud computing
Jerry's more accurate determination of abnormal address [chapter]
Realizing data value through streaming data integration (5) - flow analysis
SSH利用私钥无密钥连接服务器踩坑实录
SQL Server 递归查询上下级
JUC concurrent programming 06 -- in-depth analysis of AQS source code of queue synchronizer
Understand the new economic model of platofarm and its ecological progress
Using idea to develop Spark Program
精彩回顾 | DEEPNOVA x Iceberg Meetup Online《基于Iceberg打造实时数据湖》
Go language practice mode - functional options pattern
微信小程序中app.js文件、组件、api
0704、ansible----01
209、长度最小的子数组(数组)
DBA common SQL statements (1) - overview information
CentOS/Linux安装MySQL
Detailed explanation of MapReduce calculation process
【无标题】
微信小程序简介、发展史、小程序的优点、申请账号、开发工具、初识wxml文件和wxss文件
DBA常用SQL语句(3)- cache、undo、索引和等待事件
