剑指offer JavaScript版 (60)
2019-08-15 21:33
127 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/Damp_XUN/article/details/99656617
把二叉树打印成多行
题目描述
从上层到下层从左到右按层输出二叉树
- 用队列先进先出的性质,从左到右先遍历下层节点,到下层输出的时候,也可以从左到右输出。
- 需要记录下层节点的个数
- 需要记录当前层打印节点的个数
function Print(pRoot) { // write code here const queue=[] const res=[] if(pRoot==null){ return res; } let nextlevel=0; let curlevel=1; queue.push(pRoot); let list=[] while(queue.length!=0){ const pNode=queue.shift(); list.push(pNode.val) if(pNode.left!=null){ nextlevel++; queue.push(pNode.left) } if(pNode.right!=null){ nextlevel++; queue.push(pNode.right) } curlevel--; if(curlevel==0){ res.push(list) curlevel=nextlevel; nextlevel=0; list=[]; } } return res; }
相关文章推荐
- JMeter java.net.URISyntaxException: Illegal character in query at index 60
- cURL error 60: SSL certificate problem: unable to get local issuer certificate 解决思路
- mt2503 [MODIS]60平台上MODIS屏幕黑屏
- 软件调试笔记60 - 可调试性概览 :不可调试代码
- Can't connect to MySQL server on '****' (60)"
- java基础编程60题之2
- FAQ(60):java.sql.SQLException: Value '0000-00-00' can not be represented as java.sql.Date
- cURL error 60: SSL certificate problem: unable to get local issuer certificate...
- 【NOIP2014提高组】联合权值——从60到100
- 解决thinkphp5开发微信支付时候 遇到curl出错,错误码:60的错误
- 人工智能之深度学习应用实践60讲
- Pytorch Tutorial (1) -- DEEP LEARNING WITH PYTORCH: A 60 MINUTE BLITZ - Markdown版本
- 剑指offer 51~60
- 最全的Java笔试题库之选择题篇-(1-60)(转载)
- 新大陆NLS-CM60扫描模块平板专用
- 智慧书——永恒的处世经典(正文 1-60)
- 开始Series 60 C++程序设计(目录)
- 60 More AJAX Tutorials and 50 AJAX Toolkits and Frameworks
- Symbian60平台开发环境搭建
- LCC编译器的源程序分析(60)代码表的结构