#include<bits/stdc++.h>
using namespace std;
int ans;
unordered_map<int,int> hmap;
void postTraverse(vector<int>& post,vector<int>& in, int post_st,int post_ed,int in_st,int in_ed){
// if(post_ed > post_ed || in_st > in_ed)return;
if(post_st == post_ed || in_st == in_ed){
cout<<post[post_ed]<<endl;
return;
}
int root = post[post_ed];
// int k;
// for(int i = in_st;i<=in_ed;i++){
// if(in[i] == root){
// k = i;
// break;
// }
// }
int k = hmap[root];
int right_num = in_ed - k,left_num = k - in_st;
if(right_num > 0){
//往右走
postTraverse(post,in,post_ed - right_num, post_ed-1,k+1,in_ed);
}else{
//往左走
postTraverse(post,in,post_st,post_ed - right_num - 1,in_st,k-1);
}
}
int main(){
int n;
cin>>n;
vector<int> post(n,0);
vector<int> in(n,0);
for(int i = 0;i<n;i++) cin>>post[i];
for(int i = 0;i<n;i++){
cin>>in[i];
hmap[in[i]] = i;
}
postTraverse(post,in,0,n-1,0,n-1);
// cout<<ans;
}
算法1
(暴力枚举) $O(n^2)$
blablabla
时间复杂度
参考文献
C++ 代码
blablabla
算法2
(暴力枚举) $O(n^2)$
blablabla
时间复杂度
参考文献
C++ 代码
blablabla