#include<iostream>
#include<string>
using namespace std;
string pre, in;
struct node
{
char c;
node* leftchild;
node* rightchild;
};
node* build(int preL, int preR, int inL, int inR)
{
if (preR == preL)
return NULL;
int idx = inL;
while (in[idx] != pre[preL])
++idx;
node* root = (node*)malloc(sizeof(node));
root->c = in[idx];
int Lnum = idx - inL;
int Rnum = inR - idx - 1;
root->leftchild = build(preL + 1, preL + Lnum + 1, inL, inL + Lnum);
root->rightchild = build(preR - Rnum, preR, inR - Rnum, inR);
return root;
}
void postorder(node* root)
{
if (root == NULL)
return;
postorder(root->leftchild);
postorder(root->rightchild);
cout << root->c;
}
int main()
{
//freopen("input.txt", "r", stdin);
while (cin >> pre)
{
cin >> in;
node* root = build(0, pre.size(), 0, in.size());
postorder(root);
cout << endl;
}
}
紫书6-3,UVa536
最新推荐文章于 2024-07-25 21:13:04 发布