当前位置:网站首页>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
边栏推荐
- 402. 移掉 K 位数字-贪心
- Deep understanding of control inversion and dependency injection
- HCIP第五次实验
- Understanding and small examples of unity3d object pool
- matlab如何绘制已知公式的曲线图,Excel怎么绘制函数曲线图像?
- Summary of common SQL statements
- [C] thoroughly understand the deep copy
- C语言程序设计之函数的构造
- 48. Rotate image
- 快时钟同步慢时钟域下的异步控制信号slow clk to fast clk
猜你喜欢
基于51单片机红外无线通讯仿真
Future 用法详解
Devexpress GridView add select all columns
JVM类加载机制
Clickhouse table engine
Compare the performance of query based on the number of paging data that meet the query conditions
92. Reverse linked list II byte skipping high frequency question
Simulation of infrared wireless communication based on 51 single chip microcomputer
Halo open source project learning (II): entity classes and data tables
Tdan over half
随机推荐
Sword finger offer 22 The penultimate node in the linked list - speed pointer
Simulation of infrared wireless communication based on 51 single chip microcomputer
402. 移掉 K 位数字-贪心
Self use learning notes - connected and non connected access to database
JS failed to change all variables and changed to the return method. Finally, the problem was solved
How to use the input table one-way service to send (occupy less) picture files (body transmission)? FileReader built-in object involved
古代埃及希腊,数学用的什么进制
ros常用的函数——ros::ok(),ros::Rate,ros::spin()和ros::spinOnce()
Low code development platform sorting
JVM class loading mechanism
给 el-dialog 增加拖拽功能
How to manually implement the mechanism of triggering garbage collection in node
Exercise: even sum, threshold segmentation and difference (two basic questions of list object)
In ancient Egypt and Greece, what base system was used in mathematics
92. 反转链表 II-字节跳动高频题
Devexpress GridView add select all columns
[二叉数] 二叉树的最大深度+N叉树的最大深度
Indexes and views in MySQL
C dapper basically uses addition, deletion, modification and query transactions, etc
958. 二叉树的完全性检验