当前位置:网站首页>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
边栏推荐
- 使用BLoC 构建 Flutter的页面实例
- SLF4J 日志门面
- (construction notes) learn the specific technology of how to design reusable software entities from three levels: class, API and framework
- Vulnhub's Tomato (tomato)
- Vulnhub's presidential
- Master and backup role election strategy in kept
- Simple factory and factory method mode
- Shardingsphere sub database and sub table < 3 >
- [MySQL special] read lock and write lock
- MySQL time zone solution
猜你喜欢

Qt OpenGL 旋转、平移、缩放

PHP export word method (phpword)

STL Tutorial 9 deep copy and shallow copy of container elements

Summary of development issues

vulnhub之raven2

CGroup introduction

Duplicate numbers in the array of sword finger offer 03

Integer string int mutual conversion

Unity3d learning notes 5 - create sub mesh

使用BLoC 构建 Flutter的页面实例
随机推荐
C language improvement article (wchar_t) character type
vulnhub之raven2
Vulnhub's Tomato (tomato)
Pragma pack syntax and usage
STL tutorial 8-map
Ripper of vulnhub
Momentum of vulnhub
(构造笔记)从类、API、框架三个层面学习如何设计可复用软件实体的具体技术
Visual studio 2022 downloading and configuring opencv4.5.5
(构造笔记)ADT与OOP
win10 上PHP artisan storage:link 出现 symlink (): Protocol error的解决办法
laravel 时区问题timezone
Vulnhub narak
Talk about the state management mechanism in Flink framework
CGroup introduction
242. Effective letter heteronyms
OpenGL 索引缓存对象EBO和线宽模式
Wechat applet - basic content
Shutter: add gradient stroke to font
vulnhub之narak