当前位置:网站首页>32 JZOF 】 【 print down on binary tree
32 JZOF 】 【 print down on binary tree
2022-08-10 00:33:00 【sigh】
不分行从上往下打印出二叉树的每个节点,同层节点从左至右打印.例如输入{8,6,10,#,#,2,1},如以下图中的示例二叉树,则依次打印8,6,10,2,1(空节点不打印,跳过),请你将打印的结果存放到一个数组里面,返回.
by means of a queue.
import java.util.*;
import java.util.ArrayList;
/** public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; } } */
public class Solution {
public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {
if (root == null) {
return new ArrayList<Integer>();
}
ArrayList<Integer> res = new ArrayList<>();
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()) {
TreeNode node = queue.poll();
res.add(node.val);
if (node.left != null) {
queue.offer(node.left);
}
if (node.right != null) {
queue.offer(node.right);
}
}
return res;
}
}
边栏推荐
- VR全景结合小程序,为线上电商更好的服务
- 【Burning】It's time to show your true strength!Understand the technical highlights of the 2022 Huawei Developer Competition in one article
- 制定量化交易策略的基本步骤有哪些?
- What kind of mentality do you need to have when using the stock quantitative trading interface
- Chapter 15 HMM模型
- 【云原生】一文讲透Kubevela addon如何添加腾讯Crane
- 金仓数据库 KingbaseGIS 使用手册(6.4. 几何对象存取函数)
- 【微信小程序开发(八)】音频背景音乐播放问题汇总
- 【JZOF】77按之字形打印二叉树
- matplotlib散点图自定义坐标轴(文字坐标轴)
猜你喜欢
随机推荐
6款跨境电商常用工具汇总
What are the basic steps to develop a quantitative trading strategy?
Analyses the development status quo of stock trading
UNI-APP_ monitor page scroll h5 monitor page scroll
Basic operations of xlrd and xlsxwriter
kubesphere
深度学习100例 —— 循环神经网络(RNN)实现股票预测
DXF笔记:文字对齐的研究
制定量化交易策略的基本步骤有哪些?
iNFTnews | 迪士尼如何布局Web3
2022年最新《谷粒学院开发教程》:10 - 前台支付模块
对象深复制,面试题
国内十大活跃报表 BI 产品深度对比及点评
毕昇编译器优化:Lazy Code Motion
伦敦银行情中短线的支撑和阻力位
torch.distributed多卡/多GPU/分布式DPP(二)——torch.distributed.all_reduce(reduce_mean)&barrier&控制进程执行顺序&随机数种子
函数习题(下)
全球不用交税的国家,为什么不交
【Leetcode】2104. Sum of Subarray Ranges
【AtomicInteger】常规用法