当前位置:网站首页>二叉树的广度遍历
二叉树的广度遍历
2022-07-16 04:42:00 【疯狂的仔】
1.层次遍历
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> ans = new ArrayList<>();
if(root == null) return ans;
LinkedList<TreeNode> queue = new LinkedList<>();
queue.addLast(root);
while(!queue.isEmpty()){
int size = queue.size();
List<Integer> list = new ArrayList<>();
while(size>0){
TreeNode temp = queue.removeFirst();
list.add(temp.val);
if(temp.left!=null)queue.addLast(temp.left);
if(temp.right!=null)queue.addLast(temp.right);
size--;
}
ans.add(list);
}
return ans;
}
}边栏推荐
- Micro, m3o micro service series (I)
- What points should be paid attention to in the selection of project management system?
- 【FPGA教程案例25】通过NCO核和除法器实现tan(x)计算
- Matlab机械臂建模运动学仿真+轨迹规划
- Golang---------小试牛刀 gin框架文件上传
- [MySQL] - basic query exercise of database
- 【锁相环】基于MATLAB的全数字锁相环设计与仿真
- 文旅夜游项目助力夜间经济发展
- Filebeat collects kubernetes cluster logs
- Feign 实现服务间并且调用时传递 header
猜你喜欢

Differences between collections and collections

PMP每日一练 | 考试不迷路-7.15

【深度学习-学习笔记01】

marginalization

marginalization

Linux中安装mysql 5.7.23

联盛德W801-如何提高采集多路ADC效率

Modèles de boîtes, flux de documents, positionnement, mise en page et conception réactive

npm与node版本不匹配 更新了npm 结果报错node和npm不匹配 怎么退回之前的npm 版本?或者说怎么查看node适配的npm版本

Color supplement of MATLAB scientific research drawing (special part 6) - 336 traditional French colors
随机推荐
联盛德W801-如何提高采集多路ADC效率
Idea merges dev branch code into master and so on
【锁相环】基于MATLAB的全数字锁相环设计与仿真
Basic knowledge of triode (Part 2) ②
[target tracking] image inter frame difference target detection based on background subtraction and MATLAB simulation
JS how to realize the automatic scrolling and looping of the list
Interview problem: how to close an order without using a scheduled task?
PageRank的原理和实现
Video processing: video sampling
Preparation Notes: neural network
Iptables mask access to a port of IP
How does golang calculate constellations and zodiac signs based on birthdays
PMP practice once a day | don't get lost in the exam -7.15
Feign 实现服务间并且调用时传递 header
AB PLC learning notes
Common and practical SQL statements
T100接口开发步骤简介
idea 将dev分支代码合并到master类推
Opencv: convert video into continuous image frames
[step on the pit] resurrect Pico go