Question:題目詳情(http://vjudge.net/contest/134361#problem/F)
題目大意:給你一棵樹的前序遍歷和中序遍歷,讓你輸出後序遍歷
解題思路:就單純的建樹並輸出後序遍歷即可
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
char q[30],z[30];
void build(int L1,int R1,int L2,int R2) //建樹
{
if(L1>R1) return ;
int t;
for(t=0;z[t]!=q[L1];t++); //找到中序遍歷中根的位置
build(L1+1,L1+t-L2,L2,t-1); //先建左樹
build(L1+t-L2+1,R1,t+1,R2); //再建右樹
printf("%c",q[L1]); //最後輸出根節點
}//遞歸建樹
int main()
{
while(~scanf("%s%s",q,z))
{
int len=strlen(q)-1;
build(0,len,0,len);
printf("\n");
}
return 0;
}
體會:基本建樹,全程無套路