当前位置:网站首页>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;
}
}
边栏推荐
- 剑指 Offer 57 - II. 和为s的连续正数序列(滑动窗口)
- Explanation of RTSP protocol
- FPGA-在ISE中错误总结(更新中)
- FFmpeg多媒体文件处理(ffmpeg处理流数据的基本概念)
- 5G Unicom Network Management Design Ideas
- 为什么文字不贴合边
- 5G China unicom AP:B SMS ASCII Transcoding Requirements
- Periodic sharing of Alibaba Da Tao system model governance
- R语言kaggle 游戏数据探索与可视化
- Q_06_02 类型模型
猜你喜欢
随机推荐
联通网管协议框图
jenkins api创建自定义pipeline
蓝桥杯线上模拟赛——Flex 经典骰子布局
NFS 特别注意权限的问题
Q_06_02 类型模型
Process/Thread related in Sandbox - 1
驻波比计算方法
An Offer 21. Adjust the array in order to make odd in even the front (loop invariant)
[MRCTF2020]套娃-1
基于 R 语言的判别分析介绍与实践 LDA和QDA
Periodic sharing of Alibaba Da Tao system model governance
5G China unicom AP:B SMS ASCII 转码要求
The sword refers to Offer 57 - II. and is a continuous positive sequence of s (sliding window)
R language kaggle game data exploration and visualization
FFMPEG多媒体文件处理(ffmpeg文件的删除与重命名)
JS轮播图实现
The sword refers to Offer 56 - II. Number of occurrences of a number in an array II (bit operation)
WPF 实现带蒙版的 MessageBox 消息提示框
Time series analysis course lab report
GIN中GET POST PUT DELETE请求