当前位置:网站首页>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);
}
}```
边栏推荐
- 不要用jOOQ串联字符串
- REST会消失吗?事件驱动架构如何搭建?
- LeetCode_279_完全平方数
- JSP out. The write () method has what function?
- Axure tutorial - the new base (small white strongly recommended!!!)
- SphereEx Miao Liyao: Database Mesh R&D Practice under Cloud Native Architecture
- JSP out.write()方法具有什么功能呢?
- 双队列实现栈?双栈实现队列?
- JSP如何使用request获取当前访问者的真实IP呢?
- JSP built-in object out object function introduction
猜你喜欢

bgp aggregation reflector federation experiment

A simple file transfer tools

双队列实现栈?双栈实现队列?

不了解SynchronousQueue?那ArrayBlockingQueue和LinkedBlockingQueue不会也不知道吧?

Is TCP reliable?Why?

Detailed explanation of Zadig's self-testing and tuning environment technical solution for developers

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

Unknown CMake command “add_action_files“

链上治理为何如此重要,波卡Gov 2.0又会如何引领链上治理的发展?

GIF making - very simple one-click animation tool
随机推荐
中缀转后缀、前缀表达式快速解决办法
短视频SEO搜索运营获客系统功能介绍
An overview of the most useful DeFi tools
重装腾讯云云监控后如果对应服务不存在可通过sc.exe命令添加服务
在不完全恢复、控制文件被创建或还原后,必须使用 RESETLOGS 打开数据库,解释 RESETLOGS.
JSP如何使用request获取当前访问者的真实IP呢?
Arduino 基础语法
工业信息物理系统攻击检测增强模型
Deliver cloud-native microservices applications with Zadig
Keepalived 高可用的三种路由方案
async/await 原理及执行顺序分析
What is the function of the JSP Taglib directive?
以交易为生是一种什么体验?
How does JSP use request to get the real IP of the current visitor?
JSP request对象功能详解说明
扑克牌问题
Using the "stack" fast computing -- reverse polish expression
BGP 第一次实验
IO stream basics
为什么要使用MQ消息中间件?这几个问题必须拿下