当前位置:网站首页>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
边栏推荐
- laravel 时区问题timezone
- Test classification in openstack
- DEJA_ Vu3d - 054 of cesium feature set - simulate the whole process of rocket launch
- 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.".
- Vulnhub narak
- Colleagues wrote a responsibility chain model, with countless bugs
- (database authorization - redis) summary of unauthorized access vulnerabilities in redis
- PHP导出word方法(一mht)
- Unicode encoding table download
- Shell: basic learning
猜你喜欢
随机推荐
Groovy test class and JUnit test
Vulnhub narak
OpenGL 着色器使用
Redis notes 01: Introduction
OpenGL draws colored triangles
Kubernetes three dozen probes and probe mode
Itext7 uses iexternalsignature container for signature and signature verification
Redis 笔记 01:入门篇
Shutter: about inheritedwidget
Dynamically monitor disk i/o with ZABBIX
Introduction to the implementation principle of rxjs observable filter operator
QT OpenGL rotate, pan, zoom
Download address and installation tutorial of vs2015
Develop plug-ins for idea
Redis
OpenGL shader use
Ripper of vulnhub
CGroup introduction
[combinatorics] permutation and combination (summary of permutation and combination content | selection problem | set permutation | set combination)
php 获取文件夹下面的文件列表和文件夹列表