当前位置:网站首页>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;
}
}
边栏推荐
- 1319_STM32F103串口BootLoader移植
- OD-Model【6】:YOLOv2
- Exploration and Practice of Database Governance
- ue unreal 虚幻 高分辨率无缩放 编辑器字太小 调整编辑器整体缩放
- LayaBox---TypeScript---Example
- PRIMAL: Pathfinding via Reinforcement and Imitation Multi-Agent Learning 代码解析
- In which industries is the PMP certificate useful?
- [2022 Hangzhou Electric Power Multi-School 5 1012 Questions Buy Figurines] Application of STL
- 【线性代数02】AX=b的2种解释和矩阵乘法的5种视角
- SPSS-unary regression practice
猜你喜欢

【QT】回调函数的实现

基于声卡实现的音频存储示波器,可作为电磁学实验的测量仪表
![[2022 Nioke Duo School 5 A Question Don't Starve] DP](/img/fa/f1d11297cc5f58919bcc579f0a82e9.png)
[2022 Nioke Duo School 5 A Question Don't Starve] DP

In which industries is the PMP certificate useful?

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

立即升级!WPS Office 出现 0day 高危安全漏洞:可完全接管系统,官方推出紧急更新

ROS packages visualization

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

实战:10 种实现延迟任务的方法,附代码!

C language knowledge (1) - overview of C language, data types
随机推荐
What does Xinchuang mean?Which industries are involved?Why develop Xinchuang?
PyTorch Geometric (PyG) 安装教程
LayaBox---TypeScript---结构
js data type, throttling/anti-shake, click event delegation optimization, transition animation
打卡第 2 天: urllib简记
零基础都能拿捏的七夕浪漫代码,快去表白或去制造惊喜吧
PRIMAL: Pathfinding via Reinforcement and Imitation Multi-Agent Learning Code Analysis
如何一键重装Win11系统 一键重装系统方法
国际项目管理师PMP证书,值得考嘛?
Win11如何设置软件快捷方式?
基于 Milvus 和 ResNet50 的图像搜索(部署及应用)
dotnet compress Stream or file using lz4net
【线性代数03】消元法展示以及AX=b的4种解情况
Driving point cloud format changes bring efficiency improvement
[2022 Nioke Duo School 5 A Question Don't Starve] DP
如何根据“前序遍历,中序遍历”,“中序遍历,后序遍历”构建按二叉树
PRIMAL: Pathfinding via Reinforcement and Imitation Multi-Agent Learning 代码解析
Red team kill-free development practice of simulated confrontation
数电快速入门(三)(卡诺图化简法的介绍)
七夕特制:《牛郎会织女》