#include<cstdio> #include<cstdlib> #include<iostream> #include<cstring> #include<cmath> #define L 2000050 using namespace std; char pre[30],in[30],post[30]; int n = 0; void dfs(char *pre,char *in)//知道前序遍历,中序遍历,求后序 { if(*in == 0) return; char *t = in; int len = 0; while(*t++ != *pre) len++; //根据根在中序遍历中的位置,划分左右子树 in[len] = 0; dfs(pre+1,in);//遍历左子树 dfs(pre+len+1,in+len+1);//遍历右子树 post[n++] = *pre;//根 } int main(){ // freopen("1.txt","r",stdin); scanf("%s %s",pre,in); dfs(pre,in); post[n] = 0; puts(post); return 0; }
时间: 2024-11-07 00:34:39