当前位置:网站首页>Generate post order traversal according to pre order traversal and mid order traversal
Generate post order traversal according to pre order traversal and mid order traversal
2022-06-25 16:31:00 【GreyZeng】
author :Grey
Original address : Generate post order traversal according to pre order traversal and mid order traversal
Problem description
Cattle guest : Generate a postorder array through a preorder array and a meso array
Ideas
Suppose there is a binary tree

The result of preorder traversal is

The result of middle order traversal is

Because the scheduling logic of preorder traversal is , First , To the left , To the right
The scheduling logic of post order traversal is : First left , To the right , Re head .
therefore : The last node of post order traversal , It must be the head node of the preorder traversal .
Define recursive functions
// Traverse the array first pre Of [l1...r1] Section
// Middle order traversal array in Of [l2...r2] Section
// Generate post order traversal array pos Of [l3...r3] Section
void func(int[] pre, int l1, int r1, int[] in, int l2, int r2, int[] pos, int l3, r3)
According to the above inference , It can be concluded as follows
// The last node of post order traversal , It must be the head node of the preorder traversal
pos[r3] = pre[l1];
then , In an ordered array , We can locate the position of this header node , That is, the position marked with yellow in the figure below , Suppose this position is index,

This index The middle order array is divided into two parts , Because the scheduling process of middle order traversal is : First left , Re head , To the right , So in the middle order traversal [l2......index] Within the interval , In order to index The traversal result of the left tree with the position as the head ,[l2......index] The number of elements in the interval is assumed to be b, So in the preorder traversal , Count from top to bottom b Elements , namely :[l1......l1+b] It's made up of index The preorder traversal result of the left tree with the position as the head .
public static void func(int[] pre, int l1, int r1, int[] in, int l2, int r2, int[] pos, int l3, int r3) {
if (l1 > r1) {
// Avoidance of invalidity
return;
}
if (l1 == r1) {
// When there is only one number
pos[l3] = pre[l1];
} else {
// When there is more than one number
pos[r3] = pre[l1];
// index Indicates the position of a header in the ordered array
int index;
// Can be optimized
for (index = l2; index <= r2; index++) {
if (in[index] == pre[l1]) {
break;
}
}
int b = index - l2;
func(pre, l1 + 1, l1 + b, in, l2, index - 1, pos, l3, l3 + b - 1);
func(pre, l1 + b + 1, r1, in, index + 1, r2, pos, l3 + b, r3 - 1);
}
}
Optimize
In recursive functions func in , There is a traversal behavior ,
for (index = l2; index <= r2; index++) {
if (in[index] == pre[l1]) {
break;
}
}
If every recursion has to be traversed , Then the efficiency will be reduced , So you can set one at the beginning map, Save the location information of each value in the middle order traversal , This eliminates the need to traverse to find the location , The method is as follows :
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < n; i++) {
inOrder[i] = in.nextInt();
map.put(inOrder[i], i);
}
After this pretreatment , Every time index You don't need to traverse to get , It only needs
int index = map.get(pre[l1]);
that will do , For the full code, see
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] preOrder = new int[n];
int[] inOrder = new int[n];
for (int i = 0; i < n; i++) {
preOrder[i] = in.nextInt();
}
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < n; i++) {
inOrder[i] = in.nextInt();
map.put(inOrder[i], i);
}
int[] posOrder = new int[n];
func(preOrder, 0, n - 1, inOrder, 0, n - 1, posOrder, 0, n - 1, map);
for (int i = 0; i < n; i++) {
System.out.print(posOrder[i] + " ");
}
in.close();
}
public static void func(int[] pre, int l1, int r1, int[] in, int l2, int r2, int[] pos, int l3, int r3, Map<Integer, Integer> map) {
if (l1 > r1) {
// Avoidance of invalidity
return;
}
if (l1 == r1) {
// When there is only one number
pos[l3] = pre[l1];
} else {
// When there is more than one number
pos[r3] = pre[l1];
// index Indicates the position of a header in the ordered array
int index = map.get(pre[l1]);
int b = index - l2;
func(pre, l1 + 1, l1 + b, in, l2, index - 1, pos, l3, l3 + b - 1, map);
func(pre, l1 + b + 1, r1, in, index + 1, r2, pos, l3 + b, r3 - 1, map);
}
}
}
more
边栏推荐
- Resolve the format conflict between formatted document and eslint
- Why does golang's modification of slice data affect the data of other slices?
- 加密潮流:时尚向元宇宙的进阶
- 2021, committed to better development
- One minute to familiarize yourself with the meaning of all fluent question marks
- Introduction to MgO 256gb NAND flash chip
- Dino: Detr with improved detecting anchor boxes for end to end object detection
- Based on neural tag search, the multilingual abstracts of zero samples of Chinese Academy of Sciences and Microsoft Asiatic research were selected into ACL 2022
- Bugly hot update usage
- 10款超牛Vim插件,爱不释手了
猜你喜欢

Reverse series to obtain any wechat applet code

Android修行手册之Kotlin - 自定义View的几种写法

Nsurlsession learning notes (III) download task

加密潮流:时尚向元宇宙的进阶

Reading mysql45 lecture - index continued

What exactly is a handler

完美洗牌问题

Based on neural tag search, the multilingual abstracts of zero samples of Chinese Academy of Sciences and Microsoft Asiatic research were selected into ACL 2022

Bugly hot update usage

GO语言-什么是临界资源安全问题?
随机推荐
uniapp实现图片(单张/多张)预览
MySQL_ JDBC
Based on neural tag search, the multilingual abstracts of zero samples of Chinese Academy of Sciences and Microsoft Asiatic research were selected into ACL 2022
Tensorflow loading cifar10 dataset
cmd。。。。。。
APIJSON简单使用
Vscode有什么好用的插件?
Navicat premium 15 for MAC (database development tool) Chinese version
Activation and value transfer of activity
Learning notes of rxjs takeuntil operator
Prototype chain analysis
Lecun predicts AgI: big model and reinforcement learning are both ramps! My "world model" is the new way
【机器学习】基于多元时间序列对高考预测分析案例
Why does golang's modification of slice data affect the data of other slices?
解析数仓lazyagg查询重写优化
flutter
Unity技术手册 - 干扰/噪音/杂波(Noise)子模块
Read AFN through - from the creation of manager to the completion of data parsing
The first day of reading mysql45
Unity技术手册 - 生命周期旋转RotationOverLifetime-速度旋转RotationBySpeed-外力ExternalForces