当前位置:网站首页>LeetCode 199: 二叉树的右视图
LeetCode 199: 二叉树的右视图
2022-08-04 21:34:00 【斯沃福德】
链接
题目:
思路:BFS
使用层序遍历,每一个for就是一层;
直接将每一次for循环遍历的最后一个几点保存至List就是结果了;
class Solution {
public List<Integer> rightSideView(TreeNode root) {
List<Integer> r=new LinkedList<>();
if(root==null) return r;
// bfs
Queue<TreeNode> nodes=new LinkedList<>();
nodes.add(root);
while(!nodes.isEmpty()){
int n=nodes.size();
for(int i=0;i<n;i++){
TreeNode curr=nodes.poll();
if(i==n-1){
r.add(curr.val);
}
if(curr.left!=null){
nodes.add(curr.left);
}
if(curr.right!=null){
nodes.add(curr.right);
}
}
}
return r;
}
}
边栏推荐
- 硬件开发定制全流程解析
- 【线性代数02】AX=b的2种解释和矩阵乘法的5种视角
- deepstream多相机显示布局
- openresty lua-resty-template页面静态化
- Spss-一元回归实操
- [2022 Hangzhou Electric Multi-School 5 1003 Slipper] Multiple Super Source Points + Shortest Path
- LayaBox---knowledge point
- SPSS-unary regression practice
- Common methods of js's new Function()
- ES6高级-Promise的用法
猜你喜欢
Android 面试——如何写一个又好又快的日志库?
laravel whereDoesntHave
How to solve the problem that the alarm information cannot be transmitted after EasyGBS is connected to the latest version of Hikvision camera?
[2022 Hangzhou Electric Multi-School 5 1003 Slipper] Multiple Super Source Points + Shortest Path
ROS播包可视化
OD-Model【6】:YOLOv2
数电快速入门(三)(卡诺图化简法的介绍)
DGL安装教程
Spss-系统聚类软件实操
Hands-on Deep Learning_NiN
随机推荐
LayaBox---knowledge point
Dotnet using WMI software acquisition system installation
实战:10 种实现延迟任务的方法,附代码!
零基础都能拿捏的七夕浪漫代码,快去表白或去制造惊喜吧
【SQL之降龙十八掌】01——亢龙有悔:入门10题
硬件开发定制全流程解析
《剑指offer》刷题分类
Altium Designer 19.1.18 - 画多边形铜皮挖空时,针对光标胡乱捕获的解决方法
[2022 Hangzhou Electric Power Multi-School 5 1012 Questions Buy Figurines] Application of STL
unity2D横版游戏教程8-音效
Red team kill-free development practice of simulated confrontation
js data type, throttling/anti-shake, click event delegation optimization, transition animation
proe和creo的区别有哪些
AXI interface application of Zynq Fpga image processing - the use of axi_lite interface
PCBA方案设计——厨房语音秤芯片方案
PowerCLi 导入License到vCenter 7
y87.第五章 分布式链路追踪系统 -- 分布式链路追踪系统起源(一)
dotnet enables JIT multi-core compilation to improve startup performance
ue unreal 虚幻 高分辨率无缩放 编辑器字太小 调整编辑器整体缩放
Win11如何开启Telnet客户端?