标签:push 就是 查看 get stl iostream 换行 后序 als
//#include<bits/stdc++.h>
#include <iostream>
#include <vector>
using namespace std;
vector<int> pre, post, in;
bool unique = true;
void getIn(int preL, int preR, int postL, int postR){
if(preL == preR){
in.push_back(pre[preL]);
return;
}
if(pre[preL] == post[postR]){
int i = preL + 1;
while(i <= preR && pre[i] != post[postR-1]) i++;
if(i - preL > 1){
getIn(preL+1, i-1, postL, postL+(i-preL-1) - 1);
}else{
unique = false;
}
in.push_back(post[postR]);
getIn(i, preR, postL+(i-preL-1), postR-1);
}
}
int main(){
int n;
scanf("%d", &n);
pre.resize(n), post.resize(n);
for(int i = 0; i < n; i++){
scanf("%d", &pre[i]);
}
for(int i = 0; i < n; i++){
scanf("%d", &post[i]);
}
getIn(0, n-1, 0, n-1);
printf("%s\n%d", unique == true ? "Yes" : "No", in[0]);
for(int i = 1; i < in.size(); i++){
printf(" %d", in[i]);
}
printf("\n");
return 0;
}
A1119 Pre- and Post-order Traversals (30分)
标签:push 就是 查看 get stl iostream 换行 后序 als
原文地址:https://www.cnblogs.com/tsruixi/p/13045503.html