当前位置:网站首页>NC15 求二叉树的层序遍历
NC15 求二叉树的层序遍历
2022-08-09 13:02:00 【syc596】
层序遍历分层打印
NC15 求二叉树的层序遍历
求二叉树的层序遍历_牛客题霸_牛客网 (nowcoder.com)
102. 二叉树的层序遍历
import java.util.*;
public class Solution {
public ArrayList<ArrayList<Integer>> levelOrder (TreeNode root) {
ArrayList<ArrayList<Integer>> ret=new ArrayList<>();
if(root==null){
return ret;
}
Queue<TreeNode> q=new LinkedList<>();
q.offer(root);
while(q.isEmpty()==false){
ArrayList<Integer> list=new ArrayList<>();
int n=q.size();
for(int i=0;i<n;i++){
TreeNode cur=q.poll();
list.add(cur.val);
if(cur.left!=null){
q.offer(cur.left);
}
if(cur.right!=null){
q.offer(cur.right);
}
}
ret.add(list);
}
return ret;
}
}
边栏推荐
- IDEA Gradle 常遇问题(一)
- Oracle Recovery Tools修复空闲坏块
- 电脑重装系统还原0x80070005错误如何解决
- GIN file upload and return
- error Trailing spaces not allowed no-trailing-spaces 9:14 error Unexpected trailing comma
- 5G China unicom 一般性异常处理
- ARM板卡增加路由功能
- FFmpeg multimedia file processing (implementation of ffmpeg operation directory and list)
- 剑指 Offer 21. 调整数组顺序使奇数位于偶数前面(循环不变量)
- Yocto 可以下载的第三方库
猜你喜欢
5G Unicom Network Management Design Ideas
Anta and Huawei Sports Health jointly verify the champion running shoes and lead Chinese sports with innovation
Ledong Fire Rescue Brigade was invited to carry out fire safety training for cadres
RTSP协议讲解
技嘉显卡 RGBFusion 不能调光解决方法
阿里大淘系模型治理阶段性分享
error Trailing spaces not allowed no-trailing-spaces 9:14 error Unexpected trailing comma
缓存和数据库一致性问题
Unity3d_API_GPS_LocationService
GIN初探,环境安装
随机推荐
gin's middleware and routing grouping
error Trailing spaces not allowed no-trailing-spaces 9:14 error Unexpected trailing comma
R language kaggle game data exploration and visualization
Unicom network management protocol block diagram
FFMPEG多媒体文件处理(ffmpeg文件的删除与重命名)
WSA toolkit installed app store tip doesn't work how to solve?
43. The sword refers to Offer 1 ~ 1 the number of occurrences of n integers (recursive, mathematics)
5G China unicom 一般性异常处理
用plot_hist_numeric()实现画直方图
电脑重装系统后桌面图标如何调小尺寸
ARM board adds routing function
Q_06_03 表达式
ArcEngine(九)图形绘制
为什么文字不贴合边
The FPGA - work summary recently
01_iTween_第一天--小球抛物线
Q_06_05 文件结构
FFMPEG multimedia file processing (deletion and renaming of ffmpeg files)
Time series analysis course lab report
基于 R 语言的深度学习——简单回归案例