当前位置:网站首页>力扣-二叉树的前序遍历、中序遍历、后序遍历
力扣-二叉树的前序遍历、中序遍历、后序遍历
2022-08-05 02:00:00 【qq_52025208】
一、题目描述:给你二叉树的根节点 root ,返回它节点值的 前序 遍历。
1.递归:
class Solution {
List<Integer> list = new ArrayList<>();
public List<Integer> preorderTraversal(TreeNode root) {
if(root == null) return list;
list.add(root.val);
preorderTraversal(root.left);
preorderTraversal(root.right);
return list;
}
}
2.非递归
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> list=new ArrayList<>();
if(root==null) return list;
Stack<TreeNode> stack=new Stack<>();
stack.push(root);
while(!stack.isEmpty()){
TreeNode node= stack.pop();
list.add(node.val);
if(node.right!=null){
stack.push(node.right);
}
if(node.left!=null){
stack.push(node.left);
}
}
return list;
}
}
二、中序遍历
递归:
class Solution {
List<Integer> list = new ArrayList<>();
public List<Integer> inorderTraversal(TreeNode root) {
if(root == null) return list;
inorderTraversal(root.left);
list.add(root.val);
inorderTraversal(root.right);
return list;
}
}
非递归:
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
if(root == null) return list;
Stack<TreeNode> stack = new Stack<>();
while(root != null || !stack.empty()) {
while(root!= null) {
stack.push(root);
root = root.left;
}
root = stack.pop();
list.add(root.val);
root = root.right;
}
return list;
}
}
三、后序遍历
class Solution {
List<Integer> list = new ArrayList<>();
public List<Integer> postorderTraversal(TreeNode root) {
if(root == null) return list;
postorderTraversal(root.left);
postorderTraversal(root.right);
list.add(root.val);
return list;
}
}
边栏推荐
- [How to smash wool according to the music the couple listens to during the Qixi Festival] Does the background music affect the couple's choice of wine?
- HOG feature study notes
- 缺陷检测(图像处理部分)
- EBS uses virtual columns and hint hints to optimize sql case
- STM32使用stm32cubemx LL库系列教程
- Amazon Cloud Technology joins hands with Thundersoft to build an AIoT platform for industry customers
- pytorch的使用:使用神经网络进行气温预测
- 亚马逊云科技携手中科创达为行业客户构建AIoT平台
- Transfer Learning - Distant Domain Transfer Learning
- 编译预处理等细节
猜你喜欢
HOG特征学习笔记
如何发现一个有价值的 GameFi?
day14--postman interface test
tcp中的三次握手与四次挥手
方法重写与Object类
Programmer's list of sheep counting when insomnia | Daily anecdote
金仓数据库 KingbaseES V8 GIS数据迁移方案(3. 基于ArcGIS平台的数据迁移到KES)
Use of pytorch: Convolutional Neural Network Module
【Endnote】Word插入自定义形式的Endnote文献格式
Are testing jobs so hard to find?I am 32 this year and I have been unemployed for 2 months. What should an older test engineer do next to support his family?
随机推荐
How to create an rpm package
js中try...catch和finally的用法
软件测试技术之最有效的七大性能测试技术
【Redis】Linux下Redis安装
PHP技能评测
the mechanism of ideology
.Net C# 控制台 使用 Win32 API 创建一个窗口
How do programmers without objects spend the Chinese Valentine's Day
硬实力和软实力,哪个对测试人来说更重要?
【Unity入门计划】2D游戏中遮挡问题的处理方法&伪透视
【机器学习】21天挑战赛学习笔记(二)
pytorch的使用:使用神经网络进行气温预测
[How to smash wool according to the music the couple listens to during the Qixi Festival] Does the background music affect the couple's choice of wine?
迁移学习——Distant Domain Transfer Learning
[Unity Entry Plan] Handling of Occlusion Problems in 2D Games & Pseudo Perspective
DDOS攻击真的是无解吗?不!
安装oracle11的时候为什么会报这个问题
浅谈数据安全治理与隐私计算
CPDA|运营人如何从负基础学会数据分析(SQL)
KingbaseES V8 GIS data migration solution (2. Introduction to the capabilities of Kingbase GIS)