当前位置:网站首页>Likou - preorder traversal, inorder traversal, postorder traversal of binary tree
Likou - preorder traversal, inorder traversal, postorder traversal of binary tree
2022-08-05 02:43: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;
}
}
边栏推荐
- undo问题
- Access Characteristics of Constructor under Inheritance Relationship
- Intel XDC 2022 Wonderful Review: Build an Open Ecosystem and Unleash the Potential of "Infrastructure"
- Review 51 MCU
- 云原生(三十二) | Kubernetes篇之平台存储系统介绍
- C学生管理系统 头添加学生节点
- 1527. Patients suffering from a disease
- Snapback - same tree
- UOS系统下ksql应用缺少动态库”libtinfo.so.5“问题
- 协作D2D局部模型聚合的半分散联合学习
猜你喜欢

Matlab drawing 3

C language diary 9 3 kinds of statements of if

C language implements a simple number guessing game

2022-08-04: Input: deduplicated array arr, the numbers in it only contain 0~9.limit, a number.Return: The maximum number that can be spelled out with arr if the requirement is smaller than limit.from

【genius_platform软件平台开发】第七十六讲:vs预处理器定义的牛逼写法!!!!(其他组牛逼conding人员告知这么配置来取消宏定义)

DAY23: Command Execution & Code Execution Vulnerability

2022-08-04:输入:去重数组arr,里面的数只包含0~9。limit,一个数字。 返回:要求比limit小的情况下,能够用arr拼出来的最大数字。 来自字节。

VSCode Change Default Terminal how to modify the Default Terminal VSCode

链表的简单描述及代码的简单实现

甘特图来啦,项目管理神器,模板直接用
随机推荐
1484. Sell Products by Date
select 标签自定义样式
VSCode Change Default Terminal 如何修改vscode的默认terminal
剑指offer专项突击版第20天
Go 微服务开发框架 DMicro 的设计思路
[C language] Detailed explanation of stacks and queues (define, destroy, and data operations)
SuperMap iDesktop.Net之布尔运算求交——修复含拓扑错误复杂模型
Opening - Open a new .NET modern application development experience
【 2 】 OpenCV image processing: basic knowledge of OpenCV
解决connect: The requested address is not valid in its context
nodeJs--封装路由
mysql tree structure query problem
01 [Foreword Basic Use Core Concepts]
优炫数据库的单节点如何转集群
HDU 1114:Piggy-Bank ← 完全背包问题
627. Change of gender
QT MV\MVC结构
开源协议说明LGPL
C student management system Find student nodes based on student ID
DAY23: Command Execution & Code Execution Vulnerability