当前位置:网站首页>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;
}
}
边栏推荐
- GPIO简介
- 【自动化测试框架】关于unittest你需要知道的事
- 使用源码编译来安装PostgreSQL13.3数据库
- SQL的一种写法,匹配就更新,否则就是插入
- 使用yum来安装PostgreSQL13.3数据库
- Newsletter L Huobi ventures is in-depth contact with genesis public chain
- Who said that new consumer brands collapsed? Someone behind me won
- PostgreSQL highly available repmgr (1 master 2 slave +1witness) + pgpool II realizes master-slave switching + read-write separation
- 【212】php发送post请求有哪三种方法
- Quickly use various versions of PostgreSQL database in docker
猜你喜欢
DAY TWO
17、 MySQL - high availability + read / write separation + gtid + semi synchronous master-slave replication cluster
快讯 l Huobi Ventures与Genesis公链深入接洽中
Penetration test --- database security: detailed explanation of SQL injection into database principle
Wind chime card issuing network source code latest version - commercially available
Why is bat still addicted to 996 when the four-day working system is being tried out in Britain?
专为决策树打造,新加坡国立大学&清华大学联合提出快速安全的联邦学习新系统
Interface joint debugging test script optimization v4.0
What is AVL tree?
Who said that new consumer brands collapsed? Someone behind me won
随机推荐
Yaduo Sangu IPO
[communication] optimal power allocation in the uplink of two-layer wireless femtocell network with matlab code
专为决策树打造,新加坡国立大学&清华大学联合提出快速安全的联邦学习新系统
STM32 enters and wakes up the stop mode through the serial port
在docker中快速使用各个版本的PostgreSQL数据库
How about the order management of okcc call center
MATLIB reads data from excel table and draws function image
app通用功能測試用例
Google, Baidu and Yahoo are general search engines developed by Chinese companies_ Baidu search engine URL
Use package FY in Oracle_ Recover_ Data. PCK to recover the table of truncate misoperation
DAY TWO
(leetcode) sum of two numbers
DAY SIX
从外企离开,我才知道什么叫尊重跟合规…
MIT 6.824 - raft Student Guide
[OFDM communication] OFDM system signal detection based on deep learning with matlab code
matplotlib画柱状图并添加数值到图中
[CVPR 2022] target detection sota:dino: Detr with improved detecting anchor boxes for end to end object detection
48页数字政府智慧政务一网通办解决方案
互动滑轨屏演示能为企业展厅带来什么