当前位置:网站首页>102. 二叉树的层序遍历
102. 二叉树的层序遍历
2022-04-23 17:32:00 【hequnwang10】
一、题目描述
给你二叉树的根节点 root ,返回其节点值的 层序遍历 。 (即逐层地,从左到右访问所有节点)。
示例 1:
输入:root = [3,9,20,null,null,15,7]
输出:[[3],[9,20],[15,7]]
示例 2:
输入:root = [1]
输出:[[1]]
示例 3:
输入:root = []
输出:[]
二、解题
BFS
使用广度优先遍历,然后使用队列保存每一层的数据。BFS比较好想。
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
深度优先遍历,使用深度,每一层都创建一个新的集合,然后根据树的层数添加每一层的数据。
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){
//跳出条件
if(root == null){
return;
}
//每一层都加入一个新的集合
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://blog.csdn.net/hequnwang10/article/details/124209299
边栏推荐
- 2.Electron之HelloWorld
- Using quartz under. Net core - calendar of [6] jobs and triggers
- Advantages and disadvantages of several note taking software
- Promise (III)
- Metaprogramming, proxy and reflection
- Using quartz under. Net core - [1] quick start
- Using quartz under. Net core -- a simple trigger of [7] operation and trigger
- Shell-cut命令的使用
- [二叉数] 二叉树的最大深度+N叉树的最大深度
- El date picker limits the selection range from the current time to two months ago
猜你喜欢
stm32入门开发板选野火还是正点原子呢?
For the space occupation of the software, please refer to the installation directory
Flash project cross domain interception and DBM database learning [Baotou cultural and creative website development]
Using quartz under. Net core -- operation transfer parameters of [3] operation and trigger
JS, entries(), keys(), values(), some(), object Assign() traversal array usage
Scope and scope chain in JS
C# Task. Delay and thread The difference between sleep
How to change input into text
Perception of linear algebra 2
PC电脑使用无线网卡连接上手机热点,为什么不能上网
随机推荐
线性代数感悟之2
Tdan over half
Shell-入门、变量、以及基本的语法
Promise (II)
How to manually implement the mechanism of triggering garbage collection in node
PC uses wireless network card to connect to mobile phone hotspot. Why can't you surf the Internet
Solution of Navicat connecting Oracle library is not loaded
198. 打家劫舍-动态规划
Detailed explanation of C webpai route
1-5 nodejs commonjs specification
SiteServer CMS5. 0 Usage Summary
Excel quickly and automatically fills the contents of a row on a blank cell
C dapper basically uses addition, deletion, modification and query transactions, etc
QT modification UI does not take effect
Clickhouse SQL operation
Use of shell sed command
[二叉数] 二叉树的最大深度+N叉树的最大深度
Summary of common SQL statements
Use of shell awk command
402. 移掉 K 位数字-贪心