当前位置:网站首页>层次输出二叉树
层次输出二叉树
2022-04-23 06:25:00 【笔描相思】
var levelOrder = function(root) {
if (!root) return [];
let queue = [];
let res = [];
let level = 0;
queue.push(root);
let temp;
while (queue.length) {
res.push([]);
let size = queue.length; // 注意一下:
size--; //在层次遍历中是一个非常重要的技巧
while (size--) {
// 出队
let front = queue.shift();
res[level].push(front.val); // 入队
if (front.left) queue.push(front.left);
if (front.right) queue.push(front.right);
}
level++;
}
return res;
};
版权声明
本文为[笔描相思]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_44788119/article/details/120903223
边栏推荐
- 2022.3.14 Ali written examination
- 数据分析入门 | kaggle泰坦尼克任务(四)—>数据清洗及特征处理
- 经典套路:一类字符串计数的DP问题
- [CodeForces - 208E] Blood Cousins(k代兄弟问题)
- BTREE, B + tree and hash index
- 14. Transaction processing
- Thorough inquiry -- understanding and analysis of cocos2d source code
- [self motivation series] what really hinders you?
- 简单理解==和equals,String为什么可以不用new
- Design optimization of MySQL database
猜你喜欢

SAP SALV14 后台输出SALV数据可直接保存文件,发送Email(带排序、超链接、筛选格式)

Mysql 索引

SAP PI/PO rfc2RESTful 發布rfc接口為RESTful示例(Proxy間接法)

SAP PI/PO rfc2Soap 发布rfc接口为ws示例
![[Educational Codeforces Round 80] 解题报告](/img/54/2fd298ddce3cd3e28a8fe42b3b8a42.png)
[Educational Codeforces Round 80] 解题报告

Ogldev reading notes

超级宝典&编程指南(红蓝宝书)-读书笔记

Authorization server (simple construction of authorization server)

How to judge whether a point is within a polygon (including complex polygons or a large number of polygons)

Django使用mysql数据库报错解决
随机推荐
简单理解==和equals,String为什么可以不用new
6.聚合函数和分组统计
‘npm‘不是内部或外部命令,也不是可运行的程序 或批处理文件
技能点挖坑
2. Restricted query
自定义时间格式(YYYY-MM-DD HH:mm:ss 星期X)
Reflect on the limitations of event bus and the design and implementation of communication mechanism in component development process
页面实时显示当前时间
1.查看数据库和表
斐波拉去动态规划
3. Sort statement
SAP PI/PO rfc2Soap 发布rfc接口为ws示例
Applet newline character \ nfailure problem resolution - Daily pit stepping
数据分析入门 | kaggle泰坦尼克任务(四)—>数据清洗及特征处理
4. Multi table query
公共依赖模块common的处理
[hdu6868]Absolute Math(推式子+莫比乌斯反演)
SAP RFC_CVI_EI_INBOUND_MAIN BP主数据创建示例(仅演示客户)
BTREE, B + tree and hash index
Javscript gets the real suffix of the file