当前位置:网站首页>Leetcode sword finger offer 32 - ii Print binary tree II from top to bottom
Leetcode sword finger offer 32 - ii Print binary tree II from top to bottom
2022-07-21 02:10:00 【kt1776133839】
Title Description :
Print binary tree from top to bottom , Nodes of the same layer are printed from left to right , Print each layer to a line .
for example :
Given binary tree : [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
Return its hierarchical traversal result :
[
[3],
[9,20],
[15,7]
]
Tips :
Total number of nodes <= 1000
Their thinking :
I. Print by layer : The title requires the of binary tree Top to bottom Print ( Print by layer ), Also known as binary tree Breadth first search (BFS).BFS Usually with queue The first in first out feature of .
II. Print to one line per layer : Print all nodes of this layer to one line , And add all nodes of the next layer to the queue , And so on , It can be printed in multiple lines .
Algorithm flow :
Special case handling : When the root node is empty , Then return to the empty list [] ;
initialization : Print result list res = [] , The queue containing the root node queue = [root] ;
BFS loop : When the queue queue Jump out when empty ;
Create a new temporary list tmp , Used to store the print results of the current layer ;
Current layer print cycle : The number of cycles is the number of nodes in the current layer ( That's the queue queue length );
Out of the team : The first element out of the team , Write it down as node;
Print : take node.val Added to the tmp The tail ;
Add child nodes : if node Left of ( Right ) The child node is not empty , Turn left ( Right ) Child nodes join the queue queue ;
Results the current layer tmp Add in res .
Return value : Return to the list of print results res that will do .
Java Program :
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
Queue<TreeNode> queue = new LinkedList<>();
List<List<Integer>> res = new ArrayList<>();
if(root != null) queue.add(root);
while(!queue.isEmpty()) {
List<Integer> tmp = new ArrayList<>();
for(int i = queue.size(); i > 0; i--) {
TreeNode node = queue.poll();
tmp.add(node.val);
if(node.left != null) queue.add(node.left);
if(node.right != null) queue.add(node.right);
}
res.add(tmp);
}
return res;
}
}
边栏推荐
- 每日一题:数组中出现次数超过一半的数字(剑指Offer39)
- @[TOC](【TA-霜狼_may-《百人计划》】图形3.1 深度测试与模板测试
- 关于:在 Office 2021 中自定义模板
- DenseNet学习笔记(核心与resnet进行对比):
- Understanding and applying continuous integration Tekton
- 【AD学习记录】覆铜
- 字典序-公司命名
- 四层、七层负载均衡的区别(转)
- U.S. lawmakers advocate cracking down on encrypted mining, ringing the alarm of encryption? Only by reducing the carbon footprint can we achieve real value
- RedHat 7 network service cannot start. The problem ("device does not see to be present, delaying initialization") is handled
猜你喜欢
技术干货 | 基于 MindSpore 实现图像分割之平均表面距离
Harbor high availability cluster design and deployment (offline installation, including video)
[special for training course] Introduction to storage API
C语言文件管理函数及原理
leetcode 剑指 Offer 32 - II. 从上到下打印二叉树 II
ROS(sub,pub)测试 Plotjuggler
kettle
Fragment 这些 API 已废弃,你还在使用吗?
我有 7种 实现web实时消息推送的方案,7种!
Win: use Netsh command to configure port forwarding
随机推荐
[special for training course] start - abnormal learning notes - Code Guide
[upload range 1-11] basic level: characteristics, analysis and utilization
@[TOC](【TA-霜狼_may-《百人计划》】图形3.1 深度测试与模板测试
An interesting example to illustrate the difference of emplace_back() and push_back()
Alert log alarm "minact scn: using scan error out with error e:12751" processing
Activiti7 workflow and Alibaba components, second office OA, information management, ERP, etc
【AD学习记录】为什么原理图和PCB都在同一个文件夹下面了却,无法生成PCB?
leetcode 剑指 Offer 32 - III. 从上到下打印二叉树 III
技术干货 | 基于 MindSpore 实现 CosineSimilarity
MySQL 事务管理
【AD学习记录】覆铜
自定义MVC增查
Interpretation of new features | the restriction of MySQL 8.0 on gtid is lifted
Redis master-slave replication & sentinel mode
正则表达式
四层、七层负载均衡的区别(转)
ChromeOptions常用配置与WebUI实操
About: Customizing templates in office 2021
Three traversals of binary tree in C language
[leetcode] sword finger offer 52 The first common node of two linked lists