当前位置:网站首页>力扣-二叉树的前序遍历、中序遍历、后序遍历
力扣-二叉树的前序遍历、中序遍历、后序遍历
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;
}
}
边栏推荐
- 金仓数据库 KingbaseES V8 GIS数据迁移方案(3. 基于ArcGIS平台的数据迁移到KES)
- DDOS攻击真的是无解吗?不!
- JZ搜索引擎solr研究-从数据库创建索引
- Three handshake and four wave in tcp
- 1349. 参加考试的最大学生数 状态压缩
- 英特尔 XDC 2022 精彩回顾:共建开放生态,释放“基建”潜能
- [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?
- SuperMap支持的国产环境汇总
- 安装oracle11的时候为什么会报这个问题
- Flink 1.15.1 集群搭建(StandaloneSession)
猜你喜欢

高数_复习_第1章:函数、极限、连续

Utilities 
HOG feature study notes

iNFTnews | What can NFTs bring to the sports industry and fans?

海量服务实例动态化管理

Xunrui cms website cannot be displayed normally after relocation and server change

安装oracle11的时候为什么会报这个问题

金仓数据库 KingbaseES V8 GIS数据迁移方案(3. 基于ArcGIS平台的数据迁移到KES)

Live playback including PPT download | Build Online Deep Learning based on Flink & DeepRec
![[GYCTF2020]EasyThinking](/img/40/973411c69d1e4766d22f6a4a7c7c01.png)
[GYCTF2020]EasyThinking
随机推荐
fragment可见性判断
在这个超连接的世界里,你的数据安全吗
迁移学习——Joint Geometrical and Statistical Alignment for Visual Domain Adaptation
金仓数据库 KingbaseES V8 GIS数据迁移方案(3. 基于ArcGIS平台的数据迁移到KES)
Day Fourteen & Postman
领域驱动设计——MDD
<开发>实用工具
【Word】Word公式导出PDF后出现井号括号#()错误
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?
记录谷歌gn编译时碰到的一个错误“I could not find a “.gn“ file ...”
pytorch的使用:卷积神经网络模块
【存储】曙光存储DS800-G35 ISCSI各映射LUN给服务器
Opencv - video frame skipping processing
Greenplum数据库故障分析——能对数据库base文件夹进行软连接嘛?
网络安全与元宇宙:找出薄弱环节
意识形态的机制
SAP ERP和ORACLE ERP的区别是哪些?
开篇-开启全新的.NET现代应用开发体验
程序员失眠时的数羊列表 | 每日趣闻
“配置”是把双刃剑,带你了解各种配置方法