当前位置:网站首页>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;
}
}边栏推荐
- 电脑重装系统后桌面图标如何调小尺寸
- WPF 实现带蒙版的 MessageBox 消息提示框
- FPGA-近日工作总结
- handwritten big pile
- 5G China unicom repeater network management protocol real-time requirements
- telnet+ftp to control and upgrade the device
- Sandbox中的进程/线程相关-2
- 5G Unicom Network Management Design Ideas
- Professor Chen Qiang's "Machine Learning and R Application" course Chapter 13 Assignment
- kustomize入门示例及基本语法使用说明
猜你喜欢
随机推荐
GET POST PUT DELETE request in GIN
Yocto 可以下载的第三方库
Time series analysis course lab report
WPF 系统托盘 图标闪烁
kustomize入门示例及基本语法使用说明
FFMPEG multimedia file processing (deletion and renaming of ffmpeg files)
某高校的R语言数据分析期末作业
联通网管协议框图
5G China unicom repeater network management protocol real-time requirements
Rmarkdown教程
搭建大型分布式服务(四)Docker搭建开发环境安装Mysql
时间序列分析课程实验报告
R language kaggle game data exploration and visualization
FFmpeg multimedia file processing (ffmpeg prints audio and video Meta information)
群组行动控制--自动队列化实现策略
Data Mining-06
The sword refers to the offer, cuts the rope 2
The sword refers to Offer 56 - II. Number of occurrences of a number in an array II (bit operation)
Microsoft 10/11 命令行打开系统设置页(WUAP,!WIN32)
FFmpeg multimedia file processing (implementation of ffmpeg operation directory and list)








