【问题描述】
输入一棵二叉树的先序和中序遍历序列,输出其后序遍历序列。
【输入格式】
共两行,第一行一个字符串,表示树的先序遍历。
第二行一个字符串,表示树的中序遍历。树的结点一律用小写字母表示。
【输出格式】
仅一行,表示树的后序遍历序列。
【样例输入】
abdec
dbeac
【样例输出】
debca
代码:
#include <iostream>
using namespace std;
void calc(string sq, string sz)
{
int l1 = sq.length();
int l2 = sz.length();
int m = sz.find(sq[0]);
if (l1 > 1)
{
calc(sq.substr(1, m), sz.substr(0, m));
calc(sq.substr(m + 1, l1 - m - 1), sz.substr(m + 1, l2 - m - 1));
}
cout << sq[0];
}
int main()
{
string s1, s2;
cin >> s1 >> s2;
calc(s1, s2);
}