当前位置:网站首页>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;
}
}边栏推荐
- 现在40系显卡都快出来了,为何1060型号的显卡还有这么多人用?
- glibc 内存管理模型 释放 C库内存缓存
- An Offer 21. Adjust the array in order to make odd in even the front (loop invariant)
- 【瑞吉外卖】day05:增、删、改、查分类以及公共字段自动填充
- Anta and Huawei Sports Health jointly verify the champion running shoes and lead Chinese sports with innovation
- 剑指 Offer 57 - II. 和为s的连续正数序列(滑动窗口)
- 用plot_hist_numeric()实现画直方图
- glibc memory management model freeing C library memory cache
- IDEA Gradle 常遇问题(一)
- 微服务+微信小程序实现社区服务
猜你喜欢

GIN Bind模式获取参数和表单验证

Jenkins API groovy调用实践: Jenkins Core Api & Job DSL创建项目
![[MRCTF2020]套娃-1](/img/cb/ba780a4929acd9d76f77ab269faff6.png)
[MRCTF2020]套娃-1

01_iTween_第一天--小球抛物线

IDEA Gradle 常遇问题(一)

技嘉显卡 RGBFusion 不能调光解决方法

七夕力扣刷不停,343. 整数拆分(剑指 Offer 14- I. 剪绳子、剑指 Offer 14- II. 剪绳子 II)

现在40系显卡都快出来了,为何1060型号的显卡还有这么多人用?

FFmpeg多媒体文件处理(ffmpeg处理流数据的基本概念)

IDEA Gradle 常遇问题(二)(持续更新)
随机推荐
技嘉显卡 RGBFusion 不能调光解决方法
Process/Thread related in Sandbox - 1
glibc memory management model freeing C library memory cache
Anta and Huawei Sports Health jointly verify the champion running shoes and lead Chinese sports with innovation
Deep Learning Based on R Language - Simple Regression Case
kustomize入门示例及基本语法使用说明
面试攻略系列(四)-- 你不知道的大厂面试
Rmarkdown教程
jenkins api create custom pipeline
GIN a preliminary study, the environment is installed
【奖励公示】第23期 2022年7月奖励名单公示:社区明星评选 | 新人奖 | 博客同步 | 推荐奖
七夕力扣刷不停,343. 整数拆分(剑指 Offer 14- I. 剪绳子、剑指 Offer 14- II. 剪绳子 II)
read stream special attention
Realization of RTSP Protocol
telnet+ftp 对设备进行 操控 和 升级
5G China unicom general exception handling
FPGA-近日工作总结
Professor Chen Qiang the machine learning and R application course chapter 18 assignments
ARM板卡增加路由功能
Unicom network management protocol block diagram