当前位置:网站首页>剑指Offer 32.Ⅰ从上到下打印二叉树
剑指Offer 32.Ⅰ从上到下打印二叉树
2022-08-02 03:33:00 【HotRabbit.】
题目
从上到下打印出二叉树的每个节点,同一层的节点按照从左到右的顺序打印。
例如:
给定二叉树: [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
返回:
[3,9,20,15,7]
提示:
节点总数 <= 1000
Related Topics
- 树
- 广度优先搜索
- 二叉树
思路
BFS广度优先遍历
题解
class Solution {
public int[] levelOrder(TreeNode root) {
if (root == null){
return new int[0];
}
Queue<TreeNode> queue = new LinkedList<>();
List<Integer> list = new ArrayList<>();
queue.add(root);
while (!queue.isEmpty()){
TreeNode node = queue.poll();
list.add(node.val);
if (node.left != null){
queue.add(node.left);
}
if (node.right != null){
queue.add(node.right);
}
}
return list.stream().mapToInt(Integer::intValue).toArray();
}
}
边栏推荐
- 【TCS3200 color sensor and Arduino realize color recognition】
- Process (below): process control, termination, waiting, replacement
- 【详解】优先级队列的底层实现
- MAC安装Mysql超详细完整教程
- 分割回文串 DP+回溯 (LeetCode-131)
- 模拟电子技术------半导体
- 滑动窗口方法
- Process (in): process state, process address space
- rosdep update失败解决办法(亲测有效)
- UKlog.dat和QQ,微信文件的转移
猜你喜欢

【详解】优先级队列的底层实现

How to remotely debug PLC?

path 修补文件命令

rosdep update失败解决办法(亲测有效)

Arduino lights up nixie tubes

【plang 1.4.5】编写坦克(双人)游戏脚本

2020 - AAAI - 图像修复 Image Inpainting论文导读 -《Region Normalization for Image Inpainting》

WebApp 在线编程成趋势:如何在 iPad、Matepad 上编程?

MIPI解决方案 ICN6202:MIPI DSI转LVDS转换芯片

为什么D类音频功放可以免输出滤波器
随机推荐
78XX 79XX多路输出电源
vector的使用和模拟实现:
汇编语言跳转指令总结
The use and simulation of vector implementation:
UKlog.dat和QQ,微信文件的转移
I2C无法访问ATEC508A加密芯片问题
Cadence allegro导出Gerber文件(制板文件)图文操作
R语言 —— 多元线性回归
Based on the raspberry pie smart luggage development environment set up
最第k大的数的一般性问题
2020 - AAAI - Image Inpainting论文导读《Learning to Incorporate Structure Knowledge for Image Inpainting》
【科普贴】SPI接口详解
USB HUB USB集线器电路设计
HDMI转MIPI CSI东芝转换芯片-TC358743XBG/TC358749XBG
实现动态库(DLL)之间内存统一管理
Industry where edge gateway strong?
字符串匹配(蛮力法+KMP)
模拟电子技术------半导体
基础IO(下):软硬链接和动静态库
[Arduino connects the clock module to display the time on LCD1602]