当前位置:网站首页>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
边栏推荐
- 线性代数感悟之1
- JS, entries(), keys(), values(), some(), object Assign() traversal array usage
- Using quartz under. Net core - calendar of [6] jobs and triggers
- . net type transfer
- Using quartz under. Net core -- general properties and priority of triggers for [5] jobs and triggers
- 基于51单片机红外无线通讯仿真
- How to manually implement the mechanism of triggering garbage collection in node
- Use between nodejs modules
- Future 用法详解
- ASP. Net core dependency injection service life cycle
猜你喜欢

Advantages and disadvantages of several note taking software

Devexpress GridView add select all columns

If you start from zero according to the frame

Learning record of uni app dark horse yougou project (Part 2)

SiteServer CMS5. 0 Usage Summary

Simulation of infrared wireless communication based on 51 single chip microcomputer

Bottom processing of stack memory in browser

双指针进阶--leetcode题目--盛最多水的容器

Summary of common SQL statements

【WPF绑定3】 ListView基础绑定和数据模板绑定
随机推荐
Why do some people say SCM is simple and I have to learn it so hard?
tidb-server 的配置文件在哪里?
ASP. Net core reads the configuration file in the class library project
Exercise: even sum, threshold segmentation and difference (two basic questions of list object)
El cascade and El select click elsewhere to make the drop-down box disappear
Shell-sort命令的使用
Clickhouse SQL operation
ECMAScript history
Router object, route object, declarative navigation, programmed navigation
The system cannot be started after AHCI is enabled
Collection of common SQL statements
Scope and scope chain in JS
Promise (IV)
Come out after a thousand calls
【WPF绑定3】 ListView基础绑定和数据模板绑定
Open futures, open an account, cloud security or trust the software of futures companies?
239. 滑动窗口最大值(困难)-单向队列、大顶堆-字节跳动高频题
How to sort the numbers with text in Excel from small to large instead of the first number
. net cross platform principle (Part I)
440. 字典序的第K小数字(困难)-字典树-数节点-字节跳动高频题