当前位置:网站首页>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
边栏推荐
猜你喜欢
![Using quartz under. Net core -- operation transfer parameters of [3] operation and trigger](/img/4e/2161fc448f4af71d9b73b7de64a17f.png)
Using quartz under. Net core -- operation transfer parameters of [3] operation and trigger

stm32入门开发板选野火还是正点原子呢?

Use of todesk remote control software

JVM class loading mechanism

ASP. NET CORE3. 1. Solution to login failure after identity registers users

嵌入式系统中,FLASH中的程序代码必须搬到RAM中运行吗?

Index: teach you index from zero basis to proficient use

Deep understanding of control inversion and dependency injection

常用SQL语句总结

2. Electron's HelloWorld
随机推荐
Simulation of infrared wireless communication based on 51 single chip microcomputer
tidb-server 的配置文件在哪里?
ros常用的函数——ros::ok(),ros::Rate,ros::spin()和ros::spinOnce()
Shell-awk命令的使用
[batch change MySQL table and corresponding codes of fields in the table]
Clickhouse - data type
Change Oracle to MySQL
超分之TDAN
Model problems of stock in and stock out and inventory system
Qt 修改UI没有生效
[logical fallacy in life] Scarecrow fallacy and inability to refute are not proof
stm32入门开发板选野火还是正点原子呢?
Self use learning notes - connected and non connected access to database
[simple understanding of database]
PC电脑使用无线网卡连接上手机热点,为什么不能上网
48. 旋转图像
Deep understanding of control inversion and dependency injection
Using quartz under. Net core - [1] quick start
470. 用 Rand7() 实现 Rand10()
Using quartz under. Net core -- preliminary understanding of [2] operations and triggers