当前位置:网站首页>102. Sequence traversal of binary tree
102. Sequence traversal of binary tree
2022-04-23 17:39:00 【hequnwang10】
One 、 Title Description
Give you the root node of the binary tree root , Returns the Sequence traversal . ( That is, layer by layer , Access all nodes from left to right ).
Example 1:

Input :root = [3,9,20,null,null,15,7]
Output :[[3],[9,20],[15,7]]
Example 2:
Input :root = [1]
Output :[[1]]
Example 3:
Input :root = []
Output :[]
Two 、 Problem solving
BFS
Use breadth first traversal , Then use the queue to save the data of each layer .BFS Better to think about .
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> res = new ArrayList<>();
List<Integer> per = new ArrayList<>();
if(root == null){
return res;
}
Deque<TreeNode> queue = new LinkedList<>();
queue.add(root);
while(!queue.isEmpty()){
int size = queue.size();
for(int i = 0;i<size;i++){
TreeNode node = queue.poll();
per.add(node.val);
if(node.left != null){
queue.add(node.left);
}
if(node.right != null){
queue.add(node.right);
}
}
res.add(new ArrayList<>(per));
per = new ArrayList<>();
}
return res;
}
}
DFS
Depth-first traversal , Depth of use , Each layer creates a new collection , Then add the data of each layer according to the number of layers of the tree .
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
//DFS
List<List<Integer>> res = new ArrayList<>();
if(root == null){
return res;
}
dfs(root,0,res);
return res;
}
public void dfs(TreeNode root,int depth,List<List<Integer>> res){
// Jump out of the condition
if(root == null){
return;
}
// Each layer adds a new set
if(depth == res.size()){
res.add(new ArrayList<>());
}
res.get(depth).add(root.val);
dfs(root.left,depth+1,res);
dfs(root.right,depth+1,res);
}
}

版权声明
本文为[hequnwang10]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231732009943.html
边栏推荐
- [WPF binding 3] listview basic binding and data template binding
- Matlab / Simulink simulation of double closed loop DC speed regulation system
- Shell-cut命令的使用
- Tdan over half
- Qt 修改UI没有生效
- ASP. Net core JWT certification
- 386. Dictionary order (medium) - iteration - full arrangement
- Understanding and small examples of unity3d object pool
- Use of five routing guards
- How to change input into text
猜你喜欢
![[logical fallacy in life] Scarecrow fallacy and inability to refute are not proof](/img/71/14a17128dbe0f02edb4db3da479ef2.jpg)
[logical fallacy in life] Scarecrow fallacy and inability to refute are not proof

If you start from zero according to the frame

uni-app黑马优购项目学习记录(下)

01-初识sketch-sketch优势

2. Electron's HelloWorld

Qt error: /usr/bin/ld: cannot find -lGL: No such file or directory

Perception of linear algebra 2
![[difference between Oracle and MySQL]](/img/90/6d030a35692fa27f1a7c63985af06f.png)
[difference between Oracle and MySQL]

92. Reverse linked list II byte skipping high frequency question

快时钟同步慢时钟域下的异步控制信号slow clk to fast clk
随机推荐
[WPF binding 3] listview basic binding and data template binding
Sword finger offer 22 The penultimate node in the linked list - speed pointer
Halo open source project learning (II): entity classes and data tables
flink 学习(十二)Allowed Lateness和 Side Output
Ring back to origin problem - byte jumping high frequency problem
剑指 Offer 22. 链表中倒数第k个节点-快慢指针
1217_ Generating target files using scons
For the space occupation of the software, please refer to the installation directory
stm32入门开发板选野火还是正点原子呢?
Ouvrir des contrats à terme, ouvrir des comptes en nuage ou faire confiance aux logiciels des sociétés à terme?
394. 字符串解码-辅助栈
练习:求偶数和、阈值分割和求差( list 对象的两个基础小题)
[logical fallacy in life] Scarecrow fallacy and inability to refute are not proof
Come out after a thousand calls
Understanding and small examples of unity3d object pool
Hcip fifth experiment
Shell - introduction, variables, and basic syntax
Header built-in object
Excel quickly and automatically fills the contents of a row on a blank cell
How to use the input table one-way service to send (occupy less) picture files (body transmission)? FileReader built-in object involved