当前位置:网站首页>剑指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();
}
}
边栏推荐
猜你喜欢
随机推荐
GM8775C规格书,MIPI转LVDS,MIPI转双路LVDS分享
【操作系统】线程安全保护机制
模拟电子技术------半导体
回溯法 & 分支限界 - 2
list:list的介绍和模拟实现
C语言教程 - 制作单位转换器
电脑基本知识
Industry where edge gateway strong?
引擎开发日志:场景编辑器开发难点
Process (below): process control, termination, waiting, replacement
VCA821可变增益放大器
TC358860XBG BGA65 东芝桥接芯片 HDMI转MIPI
哈希表解题方法
USB2.0一致性测试方法_高速示波器
笔记本电脑充电问题
GM8775C MIPI转LVDS调试心得分享
Typora use
Pylon CLI 低成本的本地环境管控工具应用实例
【Connect the heart rate sensor to Arduino to read the heart rate data】
The use and simulation of vector implementation:

![[Arduino connected to GP2Y1014AU0F dust sensor]](/img/b4/c32dcd32bf5b9e8596af406c9177a2.png)







