当前位置:网站首页>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;
}
}
边栏推荐
- 【PCBA program design】Grip dynamometer program
- ROS packages visualization
- Pinduoduo open platform order information query interface [pdd.order.basic.list.get order basic information list query interface (according to transaction time)] code docking tutorial
- Develop your own text recognition application with Tesseract
- C language knowledge (1) - overview of C language, data types
- 驱动点云格式修改带来的效率提升
- Domestic PMP certificate of gold content how
- JWT actively checks whether the Token has expired
- unity2D横版游戏教程9-对话框dialog
- 命名路由、组件中name的作用
猜你喜欢

数电快速入门(三)(卡诺图化简法的介绍)

unity2D横版游戏教程8-音效

AXI interface application of Zynq Fpga image processing - the use of axi_lite interface

PowerCLi import license to vCenter 7

【PCBA方案设计】握力计方案

PyTorch Geometric (PyG) 安装教程

数电快速入门(四)(组合逻辑电路的分析以及设计的介绍)

STM32MP157A驱动开发 | 01- 板载LED作为系统心跳指示灯

Develop your own text recognition application with Tesseract

【线性代数02】AX=b的2种解释和矩阵乘法的5种视角
随机推荐
PRIMAL: Pathfinding via Reinforcement and Imitation Multi-Agent Learning Code Analysis
PMP证书在哪些行业有用?
EasyGBS接入最新版海康摄像头后无法传递告警信息该如何解决?
Spss-系统聚类软件实操
dotnet enables JIT multi-core compilation to improve startup performance
ROS packages visualization
AI/ML无线通信
SPSS-System Clustering Hand Calculation Practice
SPSS-System Clustering Software Practice
Android 面试——如何写一个又好又快的日志库?
【QT】回调函数的实现
27.降维
dotnet delete read-only files
打卡第 2 天: urllib简记
Altium Designer 19.1.18 - Protecting Locked Objects
数电快速入门(三)(卡诺图化简法的介绍)
UDP通信
unity2D横版游戏教程9-对话框dialog
How to solve the problem that the alarm information cannot be transmitted after EasyGBS is connected to the latest version of Hikvision camera?
如何一键重装win7系统?重装win7系统详细教程