当前位置:网站首页>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
边栏推荐
- First knowledge of database
- Analysis of the concept of metacosmic system
- 【 apprentissage automatique】 cas de prévision et d'analyse de l'examen d'entrée à l'Université basé sur des séries chronologiques multiples
- Day_ 04
- 2021, committed to better development
- leetcode-8. String to integer (ATOI)
- File operation, serialization, recursive copy
- The first day of reading mysql45
- What can NFT metauniverse development do?
- [untitled]
猜你喜欢

Resolve the format conflict between formatted document and eslint

Prototype chain analysis

Advanced SQL statement 1 of Linux MySQL database

Day_ ten

Day_ 18 hash table, generic

Unity技术手册 - 干扰/噪音/杂波(Noise)子模块

cmd。。。。。。

Preliminary understanding of JVM

一行代码可以做什么?

Helsinki traffic safety improvement project deploys velodyne lidar Intelligent Infrastructure Solution
随机推荐
Read AFN through - from the creation of manager to the completion of data parsing
Day_ thirteen
Day_ eleven
Once the code was encrypted by the company's computer, the compilation failed
Precautions for function default parameters (formal parameter angle)
The paid video at station B caused the up master to lose more than ten thousand fans
Data type variable operator
Uniapp converts graphic verification codes in the form of file streams into images
About the use of Aidl, complex data transmission
Function and implementation of closures
Interviewer: your resume says you are proficient in mysql, so you say cluster / Union / overlay index, table return, index push down
Lecun predicts AgI: big model and reinforcement learning are both ramps! My "world model" is the new way
Go language - lock operation
ES6 deconstruction assignment rename
The textfield is encapsulated by the flutter itself, which causes the data display to be disordered when the data in the list is updated.
【機器學習】基於多元時間序列對高考預測分析案例
Rxjs TakeUntil 操作符的学习笔记
普通人的2022春招总结(阿里、腾讯offer)
Unity技术手册 - 生命周期内大小(Size over Lifetime)和速度决定大小(Size by Speed)
Tensorflow loading cifar10 dataset