题意翻译
输入一棵二叉树的先序遍历和中序遍历序列,输出它的后序遍历序列。
输入输出样例
- 输入样例
DBACEGF ABCDEFG
BCAD CBAD
- 样例输出
ACBFGED
CDAB
【参考程序】
#include<iostream>
#include<string>
using namespace std;
void postorder(string pre,string in)
{
if(pre.size()<=0)
return;
int len=in.find(pre[0]);
postorder(pre.substr(1,len),in.substr(0,len));
postorder(pre.substr(len+1),in.substr(len+1));
cout<<pre[0];
}
int main()
{
string s1,s2;
while(cin>>s1>>s2)
{
postorder(s1,s2);
cout<<endl;
}
return 0;
}