当前位置:网站首页>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
边栏推荐
- 双闭环直流调速系统matlab/simulink仿真
- C语言程序设计之函数的构造
- 嵌入式系统中,FLASH中的程序代码必须搬到RAM中运行吗?
- Using quartz under. Net core -- general properties and priority of triggers for [5] jobs and triggers
- Excel quickly and automatically fills the contents of a row on a blank cell
- Using quartz under. Net core -- preliminary understanding of [2] operations and triggers
- 给 el-dialog 增加拖拽功能
- Abnormal resolution of Xiaomi camera
- Matlab / Simulink simulation of double closed loop DC speed regulation system
- Use of todesk remote control software
猜你喜欢
How to change input into text
. net cross platform principle (Part I)
JS, entries(), keys(), values(), some(), object Assign() traversal array usage
Use between nodejs modules
【生活中的逻辑谬误】稻草人谬误和无力反驳不算证明
快时钟同步慢时钟域下的异步控制信号slow clk to fast clk
ASP. Net core dependency injection service life cycle
Using quartz under. Net core -- general properties and priority of triggers for [5] jobs and triggers
Tdan over half
Allowed latency and side output
随机推荐
Using quartz under. Net core -- a simple trigger of [7] operation and trigger
Shell-sed命令的使用
Why do some people say SCM is simple and I have to learn it so hard?
Solution of Navicat connecting Oracle library is not loaded
[C#] 彻底搞明白深拷贝
SiteServer CMS5. 0 Usage Summary
Promise (III)
Advantages and disadvantages of several note taking software
Shell-awk命令的使用
Using quartz under. Net core -- general properties and priority of triggers for [5] jobs and triggers
Webapi + form form upload file
Use of shell cut command
Baidu Map Case - Zoom component, map scale component
1-3 nodejs installation list configuration and project environment
Clickhouse SQL operation
239. 滑动窗口最大值(困难)-单向队列、大顶堆-字节跳动高频题
Using quartz under. Net core - [1] quick start
练习:求偶数和、阈值分割和求差( list 对象的两个基础小题)
Baidu Map Case - modify map style
Clickhouse table engine