当前位置:网站首页>Knowing the inorder traversal of the array and the preorder traversal of the array, return the postorder history array
Knowing the inorder traversal of the array and the preorder traversal of the array, return the postorder history array
2022-08-02 00:20:00 【"We want to brush KouTi】
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
前言
If only one binary tree is given, it traverses the array in preorderpreand inorder traversal of the arrayin,
Can not rebuild the tree,And directly generate the post-order array of this binary tree and return it
It is known that there are no duplicate values in a binary tree
解题思路
定义f函数void类型
Traverse the array in preorder,rangeL1, R1.
Traverse the array in inorder,rangeL2, R2
Traverse the array in postorder,范围L3, R3,The three segments are of equal length
Positioning in inorder traversalXDetermine the left and right tree sizes
But the preamble is positioned,中序,后序的X后
调用递归,Generate the left tree,右树
代码
public static int[] preInToPos1(int[] pre, int[] in) {
if (pre == null || in == null || pre.length != in.length) {
return null;
}
int N = pre.length;
int[] pos = new int[N];
process1(pre, 0, N - 1, in, 0, N - 1, pos, 0, N - 1);
return pos;
}
// L1...R1 L2...R2 L3...R3
public static void process1(int[] pre, int L1, int R1, int[] in, int L2, int R2, int[] pos, int L3, int R3) {
if (L1 > R1) {
return;
}
if (L1 == R1) {
pos[L3] = pre[L1];
return;
}
pos[R3] = pre[L1];
int mid = L2;
for (; mid <= R2; mid++) {
if (in[mid] == pre[L1]) {
break;
}
}
int leftSize = mid - L2;
process1(pre, L1 + 1, L1 + leftSize, in, L2, mid - 1, pos, L3, L3 + leftSize - 1);
process1(pre, L1 + leftSize + 1, R1, in, mid + 1, R2, pos, L3 + leftSize, R3 - 1);
}
Use a hash table instead of traversing the lookup process
public static int[] zuo(int[] pre, int[] in) {
if (pre == null || in == null || pre.length != in.length) {
return null;
}
int N = pre.length;
HashMap<Integer, Integer> inMap = new HashMap<>();
for (int i = 0; i < N; i++) {
inMap.put(in[i], i);
}
int[] pos = new int[N];
func(pre, 0, N - 1, in, 0, N - 1, pos, 0, N - 1, inMap);
return pos;
}
public static void func(int[] pre, int L1, int R1, int[] in, int L2, int R2, int[] pos, int L3, int R3,
HashMap<Integer, Integer> inMap) {
if (L1 > R1) {
return;
}
if (L1 == R1) {
pos[L3] = pre[L1];
} else {
pos[R3] = pre[L1];
int index = inMap.get(pre[L1]);
func(pre, L1 + 1, L1 + index - L2, in, L2, index - 1, pos, L3, L3 + index - L2 - 1, inMap);
func(pre, L1 + index - L2 + 1, R1, in, index + 1, R2, pos, L3 + index - L2, R3 - 1, inMap);
}
}```
边栏推荐
- 一篇永久摆脱Mysql时区错误问题,idea数据库可视化插件配置
- 一文概览最实用的 DeFi 工具
- 【加密周报】经济衰退在加息气氛中蔓延 美联储“放手一搏”?盘点上周加密市场发生的重大事件
- els 方块边界变形处理
- A simple file transfer tools
- How to find new potential projects?Tools recommended
- JSP request对象功能详解说明
- Is TCP reliable?Why?
- GIF making - very simple one-click animation tool
- Detailed explanation of Zadig's self-testing and tuning environment technical solution for developers
猜你喜欢

How to find new potential projects?Tools recommended

Keepalived 高可用的三种路由方案

Quick solution for infix to suffix and prefix expressions

当奈飞的NFT忘记了Web2的业务安全

QML package management

SphereEx Miao Liyao: Database Mesh R&D Practice under Cloud Native Architecture

解析正则表达式的底层实现原理

Using the "stack" fast computing -- reverse polish expression

146. LRU cache

Task execution control in Ansible
随机推荐
Don't know about SynchronousQueue?So ArrayBlockingQueue and LinkedBlockingQueue don't and don't know?
【解决】win10下emqx启动报错Unable to load emulator DLL、node.db_role = EMQX_NODE__DB_ROLE = core
构造方法,this关键字,方法的重载,局部变量与成员变量
Unity—四元数、欧拉角API+坐标系统
bgp 聚合 反射器 联邦实验
After an incomplete recovery, the control file has been created or restored, the database must be opened with RESETLOGS, interpreting RESETLOGS.
How to reinstall Win11?One-click method to reinstall Win11
不要用jOOQ串联字符串
LeetCode_279_完全平方数
Collection of NFT tools
重装腾讯云云监控后如果对应服务不存在可通过sc.exe命令添加服务
An Enhanced Model for Attack Detection of Industrial Cyber-Physical Systems
众筹DAO“枯萎”的缩影:曾拍下《沙丘》未出版手稿的Spice DAO解散
Industrial control network intrusion detection based on automatic optimization of hyperparameters
Simpson's paradox
JSP如何使用page指令让JSP文件支持中文编码呢?
How to design a circular queue?Come and learn~
08-SDRAM: Summary
[21-Day Learning Challenge] A small summary of sequential search and binary search
电机原理动图合集