当前位置:网站首页>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;
}
}
边栏推荐
- C language knowledge (1) - overview of C language, data types
- 国际项目管理师PMP证书,值得考嘛?
- dotnet enables JIT multi-core compilation to improve startup performance
- 2022年江苏省大学生电子设计竞赛(TI杯)B题 飞机 省级一等奖记录 “一个摆烂人的独白”
- [2022 Hangzhou Electric Multi-School 5 1003 Slipper] Multiple Super Source Points + Shortest Path
- 【PCBA方案设计】握力计方案
- [larave]关于laravel使用form submit()不能获取值问题
- JWT actively checks whether the Token has expired
- LayaBox---knowledge point
- 传奇服务器需要什么配置?传奇服务器租用价格表
猜你喜欢
C语言知识大全(一)——C语言概述,数据类型
EasyGBS接入最新版海康摄像头后无法传递告警信息该如何解决?
C language knowledge (1) - overview of C language, data types
模拟对抗之红队免杀开发实践
Red team kill-free development practice of simulated confrontation
buu web
零基础都能拿捏的七夕浪漫代码,快去表白或去制造惊喜吧
Develop your own text recognition application with Tesseract
Qiangwang Cup 2022 - WEB
Win11如何设置软件快捷方式?
随机推荐
SPSS-System Clustering Software Practice
ES6高级-Promise的用法
【Programming Ideas】
【PCBA program design】Grip dynamometer program
ue unreal 虚幻 高分辨率无缩放 编辑器字太小 调整编辑器整体缩放
打卡第 2 天: urllib简记
deepstream多相机显示布局
LayaBox---TypeScript---结构
Unknown point cloud structure file conversion requirements
UDP communication
[2022 Nioke Duo School 5 A Question Don't Starve] DP
UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xd6 in position 120: invalid continuation byte
Yolov7:Trainable bag-of-freebies sets new state-of-the-art for real-time objectdetectors
LayaBox---TypeScript---Example
MySQL查询为啥慢了?
"Jianzhi offer" brush title classification
NFT宝典:你需要知道NFT的术语和定义
Arduino 电机测速
LayaBox---知识点
Zynq Fpga图像处理之AXI接口应用——axi_lite接口使用