当前位置:网站首页>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
边栏推荐
- SystemVerilog -- OOP -- copy of object
- Sheet1$. Output [excel source output] Error in column [xxx]. The returned column status is: "the text is truncated, or one or more characters have no matches in the target code page.".
- Socket TCP for network communication (I)
- Redis notes 01: Introduction
- (construction notes) grasp learning experience
- [official MySQL document] deadlock
- (构造笔记)MIT reading部分学习心得
- Systemverilog-- OOP--对象的拷贝
- OpenGL index cache object EBO and lineweight mode
- Master and backup role election strategy in kept
猜你喜欢

Qt+vtk+occt reading iges/step model

使用BLoC 构建 Flutter的页面实例

PHP导出word方法(一phpword)

During FTP login, the error "530 login incorrect.login failed" is reported

ES6 standard

Raven2 of vulnhub

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

Itext7 uses iexternalsignature container for signature and signature verification

(construction notes) ADT and OOP

OpenGL index cache object EBO and lineweight mode
随机推荐
[combinatorics] permutation and combination (summary of permutation and combination content | selection problem | set permutation | set combination)
Niuniu's team competition
Talk about the state management mechanism in Flink framework
OpenGL 着色器使用
安装electron失败的解决办法
(construction notes) learning experience of MIT reading
【mysql官方文档】死锁
PHP get the file list and folder list under the folder
typeScript
Oracle advanced (I) realize DMP by expdp impdp command
Raven2 of vulnhub
Solutions to the failure of installing electron
Qt OpenGL相机的使用
Symlink(): solution to protocol error in PHP artisan storage:link on win10
Itext7 uses iexternalsignature container for signature and signature verification
Flutter Widget : Flow
Quantitative calculation research
Duplicate numbers in the array of sword finger offer 03
Pragma pack syntax and usage
QT OpenGL texture map