当前位置:网站首页>Leecode brush questions record interview questions 32 - I. print binary tree from top to bottom
Leecode brush questions record interview questions 32 - I. print binary tree from top to bottom
2022-07-07 00:12:00 【Why is there a bug list】
topic
Print out each node of the binary tree from top to bottom , Nodes of the same layer are printed from left to right .
for example :
Given binary tree : [3,9,20,null,null,15,7],
3
/
9 20
/
15 7
return :
[3,9,20,15,7]
Tips :
Total number of nodes <= 1000
answer
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */
import java.util.ArrayList;
import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;
class Solution {
public int[] levelOrder(TreeNode root)
{
if(root == null)
{
int[] a = {
};
return a;
}
Queue<TreeNode> arrayBlockingQueue = new ArrayBlockingQueue<TreeNode>(1000);
ArrayList<Integer> result = new ArrayList<>();
arrayBlockingQueue.add(root);
while (!arrayBlockingQueue.isEmpty())
{
TreeNode treeNode = arrayBlockingQueue.poll();
result.add(treeNode.val);
if(treeNode.left != null) arrayBlockingQueue.add(treeNode.left);
if(treeNode.right != null) arrayBlockingQueue.add(treeNode.right);
}
int[] sz = new int[result.size()];
for(int i = 0 ;i < result.size(); i ++)
{
sz[i] = result.get(i);
}
return sz;
}
}
边栏推荐
- Design a red envelope grabbing system
- Basic chart interpretation of "Oriental selection" hot out of circle data
- "Latex" Introduction to latex mathematical formula "suggestions collection"
- Introduction au GPIO
- Things like random
- GPIO简介
- Hydrogen future industry accelerates | the registration channel of 2022 hydrogen energy specialty special new entrepreneurship competition is opened!
- 互动滑轨屏演示能为企业展厅带来什么
- MVC and MVVM
- 1000字精选 —— 接口测试基础
猜你喜欢
随机推荐
The "white paper on the panorama of the digital economy" has been released with great emphasis on the digitalization of insurance
Common modification commands of Oracle for tables
pinia 模块划分
Pytest multi process / multi thread execution test case
刘永鑫报告|微生物组数据分析与科学传播(晚7点半)
vector的使用方法_vector指针如何使用
matplotlib画柱状图并添加数值到图中
快讯 l Huobi Ventures与Genesis公链深入接洽中
Use source code compilation to install postgresql13.3 database
openresty ngx_ Lua subrequest
《LaTex》LaTex数学公式简介「建议收藏」
使用源码编译来安装PostgreSQL13.3数据库
app通用功能測試用例
2022/2/12 summary
从外企离开,我才知道什么叫尊重跟合规…
自动化测试工具Katalon(Web)测试操作说明
[unmanned aerial vehicle] multi unmanned cooperative task allocation program platform, including Matlab code
零代码高回报,如何用40套模板,能满足工作中95%的报表需求
How does win11 restore the traditional right-click menu? Win11 right click to change back to traditional mode
基于jsp+servlet+mysql框架的旅游管理系统【源码+数据库+报告】









