当前位置:网站首页>Yyds dry inventory solution sword finger offer: print the binary tree into multiple lines
Yyds dry inventory solution sword finger offer: print the binary tree into multiple lines
2022-06-23 00:40:00 【51CTO】
1. sketch :
describe
Given the number of nodes is n Binary tree , It is required to print the binary tree layer by layer from top to bottom val value , The nodes of the same layer output from left to right , Output one line per layer , Store the output results in a two-dimensional array and return . for example :
The given binary tree is {1,2,3,#,#,4,5}

The result of traversal of the binary tree is [[1],[2,3],[4,5]]
Data range : The number of nodes in a binary tree ,
requirement : Spatial complexity
, Time complexity
Input description :
Given the root node of a binary tree
Example 1
Input :
Return value :
Example 2
Input :
Return value :
Example 3
Input :
Return value :
Example 4
Input :
Return value :
2. Code implementation :
import java.util.*;
public class Solution {
// Level traversal
public ArrayList<ArrayList<Integer> > Print(TreeNode pRoot) {
TreeNode head = pRoot;
ArrayList<ArrayList<Integer> > res = new ArrayList<ArrayList<Integer> >();
if(head == null)
// If it's empty , It returns an empty array directly
return res;
// Queue storage , Perform hierarchical traversal
Queue<TreeNode> temp = new LinkedList<TreeNode>();
temp.offer(head);
TreeNode p;
while(!temp.isEmpty()){
// Record a row of a binary tree
ArrayList<Integer> row = new ArrayList<Integer>();
int n = temp.size();
// Because the first entry is the root node , Therefore, the number of nodes in each layer , The size of the queue is
for(int i = 0; i < n; i++){
p = temp.poll();
row.add(p.val);
// If left and right children exist , Then save the left and right children as the next level
if(p.left != null)
temp.offer(p.left);
if(p.right != null)
temp.offer(p.right);
}
res.add(row);
}
return res;
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
边栏推荐
- TIDB监控升级解决panic的漫漫探索之路
- Is it safe for CICC securities to open an account? What is its relationship with CICC?
- [machine learning watermelon book] update challenge [Day1]: 1.1 INTRODUCTION
- Kunlun distributed database technology advantages
- Express, route, request object, response object, middleware, EJS template
- What do you pay special attention to when you insert / update / delete / obtain millions of rows of data in a DML statement?
- Does qiniu school belong to a securities company? Is it safe to open an account?
- SAP UI5 应用开发教程之一百零二 - SAP UI5 应用的打印(Print)功能实现详解
- ROS2暑期学校 ROS2 Summer School 2022-转-
- Dig three feet to solve the data consistency problem between redis and MySQL
猜你喜欢

Analysis on the wallet system architecture of Baidu trading platform

语义分割新范式!StructToken:对per-pixel 分类范式的重新思考

OLAP - Druid introduction

Mysql8.0 easily completes gtid master-slave replication

2022 TIANTI match - National Finals rematch

Introduction to the unique variable reading and writing function of Kunlun distributed database

TiDB VS MySQL

XML escape character cross reference table

Tidb monitoring upgrade: a long way to solve panic

SAP MM ME27 创建公司内STO单
随机推荐
XML escape character cross reference table
How to set the power-off auto start of easycvr hardware box
声网多人视频录制与合成支持掉线再录制 | 掘金技术征文
Hierarchy selector
Is it safe to invest in funds through daily funds? I intend to open an account to buy funds
“听觉”营销价值凸显,喜马拉雅迎来新局点
【GO】Go Modules入门
初学者如何快速入门深度学习?
cadence SPB17.4 - allegro - 优化指定单条电气线折线连接角度 - 折线转圆弧
Package management tools --npm, -cnpm, -yan, -cyarn
层次选择器
你踩过这些坑吗?谨慎在时间类型列上创建索引
[machine learning watermelon book] update challenge [Day1]: 1.1 INTRODUCTION
Several abnormal scenarios of things system
What is the storage structure and mode of data in the database?
SAP MM ME27 创建公司内STO单
Analysis on the wallet system architecture of Baidu trading platform
Express、路由(Route)、Request对象、Response对象、中间件、EJS模板
中国国际期货有限公司怎么样,是正规的期货公司吗?网上开户安全吗?
#yyds干货盘点#尾递归比递归好在哪儿