当前位置:网站首页>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 lengthPositioning 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);
}
}```
边栏推荐
- 22. The support vector machine (SVM), gaussian kernel function
- PHP从txt文件中读取数据的方法
- Transient Stability Distributed Control of Power System with External Energy Storage
- 协作乐高 All In One:DAO工具大全
- Simpson's paradox
- security cross-domain configuration
- 使用jOOQ将Oracle风格的隐式连接自动转换为ANSI JOIN
- 已知中序遍历数组和先序遍历数组,返回后序遗历数组
- 08-SDRAM:汇总
- 【加密周报】经济衰退在加息气氛中蔓延 美联储“放手一搏”?盘点上周加密市场发生的重大事件
猜你喜欢
随机推荐
使用jOOQ将Oracle风格的隐式连接自动转换为ANSI JOIN
uni-app项目总结
C language Qixi is coming!It's time to show the romance of programmers!
面试高频考题解法——栈的压入弹出序列、有效的括号、逆波兰表达式求值
[头条]笔试题——最小栈
GetHashCode方法与=
短视频SEO搜索运营获客系统功能介绍
What is the function of the JSP out.println() method?
Keepalived 高可用的三种路由方案
面试必问的HashCode技术内幕
[21-Day Learning Challenge] A small summary of sequential search and binary search
PHP从txt文件中读取数据的方法
当奈飞的NFT忘记了Web2的业务安全
Arduino Basic Syntax
为什么要使用MQ消息中间件?这几个问题必须拿下
Difference between JSP out.print() and out.write() methods
【21天学习挑战赛】顺序查找和二分查找的小总结
632. 最小区间
bgp 聚合 反射器 联邦实验
security session concurrency management