当前位置:网站首页>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);
}
}```
边栏推荐
- Interview high-frequency test questions solution - stack push and pop sequence, effective parentheses, reverse Polish expression evaluation
- 路由策略
- 链上治理为何如此重要,波卡Gov 2.0又会如何引领链上治理的发展?
- Arduino Basic Syntax
- 07-SDRAM :FIFO控制模块
- Double queue implementation stack?Dual stack implementation queue?
- Study Notes: The Return of Machine Learning
- A simple file transfer tools
- async/await 原理及执行顺序分析
- 信息物理系统状态估计与传感器攻击检测
猜你喜欢

面试必问的HashCode技术内幕

接地气讲解TCP协议和网络程序设计

IP核:FIFO

TCL:在Quartus中使用tcl脚本语言进行管脚约束

短视频seo搜索优化主要内容

Short video SEO search operation customer acquisition system function introduction

Short video SEO optimization tutorial Self-media SEO optimization skills and methods

一文概览最实用的 DeFi 工具

如何设计循环队列?快进来学习~

Keepalived 高可用的三种路由方案
随机推荐
回顾历史5次经济衰退时期:这一次可能会有何不同?
Unity—四元数、欧拉角API+坐标系统
ROS dynamic parameters
An overview of the most useful DeFi tools
Short video SEO optimization tutorial Self-media SEO optimization skills and methods
Unknown CMake command “add_action_files“
基于相关性变量筛选偏最小二乘回归的多维相关时间序列建模方法
Short video SEO search operation customer acquisition system function introduction
Axure tutorial - the new base (small white strongly recommended!!!)
中缀转后缀、前缀表达式快速解决办法
How to solve the error when mysql8 installs make
玩转NFT夏季:这份工具宝典值得收藏
bgp aggregation reflector federation experiment
JSP request对象功能详解说明
基于注意力机制的多特征融合人脸活体检测
REST会消失吗?事件驱动架构如何搭建?
单片机遥控开关系统设计(结构原理、电路、程序)
Redis-消息发布订阅
Transient Stability Distributed Control of Power System with External Energy Storage
LeetCode_279_完全平方数