当前位置:网站首页>NC15 求二叉树的层序遍历
NC15 求二叉树的层序遍历
2022-07-25 18:13: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;
}
}边栏推荐
猜你喜欢

Imx6 rtl8189ftv migration

Talking about Devops monitoring, how does the team choose monitoring tools?

SQL那些事

Lwip之内存与包缓冲管理

1---电子实物认知

Nineteen year old summary

图的相关操作

Drawing PDF tables (I) drawing excel table styles in PDF and downloading them through iText (supporting Chinese fonts)

Redis source code and design analysis -- 16. AOF persistence mechanism

Landmark buildings around the world
随机推荐
MATLAB中join函数使用
Oracle import error: imp-00038: unable to convert to environment character set handle
BiSeNet v1
Idea integrates common functions of SVN code management
UFT(QTP)-总结点与自动化测试框架
CH582 BLE 5.0 使用 LE Coded 广播和连接
Talking about Devops monitoring, how does the team choose monitoring tools?
408第二章线性表
Optimistic lock pessimistic lock applicable scenario
喜讯!瑞云科技被授予“海上扬帆”5G融合应用专委会成员单位
Postman get started quickly
如何判断静态代码质量分析工具的性能?这五大因素必须考虑
SDLC software development life cycle and model
What is the relationship between cloud fluidization and cloud desktop
Mock service Moco series (I) - introduction, first demo, get request, post request
Redis source code and design analysis -- 18. Analysis of redis network connection Library
云流化和云桌面有什么关系
What scenarios have rust, which is becoming more and more mature, applied?
Bl602 development environment setup
SDLC 软件开发生命周期及模型