当前位置:网站首页>层次输出二叉树
层次输出二叉树
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
边栏推荐
- typescript字典的使用
- redis连接出错 ERR AUTH <password> called without any password configured for the default user.
- SAP ECC连接SAP PI系统配置
- 安装配置淘宝镜像npm(cnpm)
- 如何SQL 语句UNION实现当一个表中的一列内容为空时则取另一个表的另一列
- 数据库查询优化的方式
- Failed to install Tui editor, quick solution
- 超级宝典&编程指南(红蓝宝书)-读书笔记
- MySQL isolation level
- ‘npm‘不是内部或外部命令,也不是可运行的程序 或批处理文件
猜你喜欢
王者荣耀-unity学习之旅
h5本地存储数据sessionStorage、localStorage
页面动态显示时间(升级版)
SAP pi / PO rfc2soap publishes RFC interface as WS example
反思 | Android 音视频缓存机制的系统性设计
H5 case development
keytool: command not found
ABAP CDS VIEW WITH ASSOCIATION示例
Design optimization of MySQL database
SAP 03-AMDP CDS Table Function using ‘WITH‘ Clause(Join子查询内容)
随机推荐
[牛客练习赛68]牛牛的粉丝(矩阵快速幂之循环矩阵优化)
Reflect on the limitations of event bus and the design and implementation of communication mechanism in component development process
Super classic & Programming Guide (red and blue book) - Reading Notes
[hdu6833]A Very Easy Math Problem(莫比乌斯反演)
5.SQL99标准:内连接和外连接
CSDN很火的汤小洋老师全部课程总共有哪些(问号问号问号)
手游性能优化
ABAP 实现发布RESTful服务供外部调用示例
Ogldev reading notes
关于素数的不到100个秘密
Moment.js中format方法函数的格式
ABAP 7.4 SQL Window Expression
数论分块(整除分块)
公共依赖模块common的处理
ogldev-读书笔记
自定义时间格式(YYYY-MM-DD HH:mm:ss 星期X)
FSM有限状态机
数据库查询优化的方式
3.排序语句
SAP TRANSLATE使用数据对象掩码示例