当前位置:网站首页>Preorder, middle order and postorder traversal of binary tree
Preorder, middle order and postorder traversal of binary tree
2022-07-19 05:36:00 【Lun Zhi】
Input {1,2,3}
Output
[[1,2,3],[2,1,3],[2,3,1]]
java
public class Solution {
/**
*
* @param root TreeNode class the root of binary tree
* @return int Two dimensional arrays of integers
*/
List<Integer> front = new ArrayList<>();
List<Integer> mid = new ArrayList<>();
List<Integer> back = new ArrayList<>();
public int[][] threeOrders (TreeNode root) {
// write code here
PreorderTraversal(root);
InorderTraversal(root);
PostorderTraversal(root);
int[][] res = {
front.stream().mapToInt(Integer::valueOf).toArray(),
mid.stream().mapToInt(Integer::valueOf).toArray(),
back.stream().mapToInt(Integer::valueOf).toArray()
};
return res;
}
// The former sequence traversal
public void PreorderTraversal(TreeNode root){
if(root == null)
return;
front.add(root.val);
PreorderTraversal(root.left);
PreorderTraversal(root.right);
}
// In the sequence traversal
public void InorderTraversal(TreeNode root){
if(root == null)
return;
InorderTraversal(root.left);
mid.add(root.val);
InorderTraversal(root.right);
}
// After the sequence traversal
public void PostorderTraversal(TreeNode root){
if(root == null)
return;
PostorderTraversal(root.left);
PostorderTraversal(root.right);
back.add(root.val);
}
}
边栏推荐
- C语言的高级操作
- Interviewer: how to solve the problem of a large number of requests for data that does not exist in redis, which affects the database?
- 10 question 10 answer: do you really know thread pools?
- What is the employment prospect of software testing? There is a large demand for talents and strong job stability
- 5.数据采集通道搭建之业务分析
- 操作系统常见面试题
- JNI实用笔记
- MYSQL基本语法字典
- Ambari2.7.5 integration es6.4.2
- 柠檬班软件测试培训可靠吗 这个从培训班逆袭成功的案例告诉你
猜你喜欢
LiveData浅析
Interviewer: how to solve the problem of a large number of requests for data that does not exist in redis, which affects the database?
利用IDE打jar包
gradle自定义插件
5.1 business data acquisition channel construction of data acquisition channel construction
C语言&位域
7. Data warehouse environment preparation for data warehouse construction
Operation of C language files
跨域和处理跨域
【全网首发】主线程异常会导致 JVM 退出?
随机推荐
5. Business analysis of data acquisition channel construction
ambari 2.7.5集成安装hue 4.6
Online software testing training institutions lemon class and itest AI platform achieves strategic cooperation
【函数的效率】
递归的应用
MySQL 查询当天、本周,本月、上一个月的数据
throttle/debounce应用及原理
3.东软跨境电商数仓项目架构设计
BottomSheetDialogFragment仿制抖音评论框
sqlalchemy的两种方法详解
idea导入本地包
10 question 10 answer: do you really know thread pools?
The latest news of spring recruitment in 2022: the average salary of it Internet industry is 18500 yuan
MySQL--触发器与视图
C语言的高级操作
7.数据仓库搭建之数据仓库环境准备
Parent components plus scoped sometimes affect child components
9. Dim layer construction of data warehouse construction
【Bug解决】org.apache.ibatis.type.TypeException: The alias ‘xxxx‘ is already mapped to the value ‘xxx‘
表字段属性查询