已知二叉树的的先序和中序遍历,求后序遍历。
#include<bits/stdc++.h>
using namespace std;
const int maxn=50;
int lch[maxn],rch[maxn];
char in_order[maxn],pre_order[maxn];
void build(int L1, int R1,int L2,int R2) {
if(L1>R1) return ;
char root = pre_order[L1];
int p=L2;
while(in_order[p]!=root) p++;
build(L1+1, L1+p-L2, L2, p-1);
build(L1+p+1-L2, R1, p+1, R2);
cout<<root;
}
int main() {
while(scanf("%s",pre_order)!=EOF) {
scanf("%s",in_order);
int n=strlen(in_order);
build(0,n-1,0,n-1);
cout<<'\n';
}
return 0;
}