当前位置:网站首页>102. Sequence traversal of binary tree
102. Sequence traversal of binary tree
2022-07-03 12:12:00 【zwanying】
Sequence traversal : From left to right , Output the node value of the binary tree from top to bottom .
Force link 102. Sequence traversal of binary tree
Method 1 : recursive
Element 1 :
Parameters :
(1) Traversing nodes TreeNode
(2) The layer number int Used to identify which layer is being traversed
(3)List Node used to store traversal
Return value : empty
Element 2 :
The end condition : Node is empty
Element three :
Recursive logic :
If you are traversing the first node of this hierarchy , Building new list Store this layer of data .
Store the node values being traversed .
Go through the next layer : Recursively traverse its left node , Right node
Concrete realization
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> lists = new ArrayList<>();
if(root == null) return lists;
int level = 1;
digui(root,level,lists);
return lists;
}
void digui(TreeNode root,int level,List<List<Integer>> lists){
if(root == null) return ;
if(level==lists.size()+1){
List<Integer> list = new ArrayList();
lists.add(list);
}
lists.get(level-1).add(root.val);
digui(root.left,level+1,lists);
digui(root.right,level+1,lists);
}
}
Method 2 iteration
Implement... With queues , Enter first , Go out first .
Key points : How to calculate the number of layers ?
1、 Traverse the first layer , Number of layers plus one , Add all child nodes of this layer to the queue .
2、 Traverse all child nodes added in the previous step in the queue , Add their children to the queue . Traverse complete , Number of layers plus one .
3、 Continue with the above steps
边栏推荐
- Solutions to the failure of installing electron
- Wrong arrangement (lottery, email)
- PHP导出word方法(一phpword)
- PHP export word method (one MHT)
- Php Export word method (One MHT)
- vulnhub之narak
- OPenGL 基本知识(根据自己理解整理)
- Solution to the second weekly test of ACM intensive training of Hunan Institute of technology in 2022
- New features of ES6
- OpenGL index cache object EBO and lineweight mode
猜你喜欢

Duplicate numbers in the array of sword finger offer 03

STL tutorial 10 container commonalities and usage scenarios

ES6 standard

vulnhub之Nagini

Qt OpenGL 纹理贴图

Basic knowledge of OpenGL (sort it out according to your own understanding)

Shutter: overview of shutter architecture (excerpt)

Niuniu's team competition

During FTP login, the error "530 login incorrect.login failed" is reported
![[learning notes] DP status and transfer](/img/5e/59c64d2fe08b89fba2d7e1e6de2761.png)
[learning notes] DP status and transfer
随机推荐
(構造筆記)從類、API、框架三個層面學習如何設計可複用軟件實體的具體技術
Oracle advanced (I) realize DMP by expdp impdp command
Flutter Widget : KeyedSubtree
(construction notes) learn the specific technology of how to design reusable software entities from three levels: class, API and framework
vulnhub之Ripper
vulnhub之Nagini
023(【模板】最小生成树)(最小生成树)
vulnhub之GeminiInc v2
实现验证码验证
Experience container in libvirt
(数据库提权——Redis)Redis未授权访问漏洞总结
Redis notes 01: Introduction
MySQL time zone solution
Solve msvcp120d DLL and msvcr120d DLL missing
网上炒股开户安不安全?谁给回答一下
Cacti monitors redis implementation process
OpenGL index cache object EBO and lineweight mode
How to deploy web pages to Alibaba cloud
Talk about the state management mechanism in Flink framework
(构造笔记)ADT与OOP