POJ 2255
//由先中序建树,然后后序遍历
#include <cstring>
#include <string>
#include <iostream>
#include <cstdlib>
using namespace std;
typedef struct Node
{
char data;
Node *lchild,*rchild;
}Node,*Bitree;
Bitree creat(string s1,string s2)
{
if(s1.length()==0)//到叶子节点
return NULL;
Node *root = new Node;
if(!root)
exit(-1);
root->data=s1[0];
size_t pos = s2.find(s1[0]);
root->lchild=creat(s1.substr(1,pos),s2.substr(0,pos));
root->rchild=creat(s1.substr(pos+1),s2.substr(pos+1));
return root;
}
void postorder(Node *root)
{
if(root)
{
postorder(root->lchild);
postorder(root->rchild);
cout<<root->data;
}
}
int main()
{
string s1,s2;
while(cin>>s1>>s2)
{
Node *root;
root=creat(s1,s2);
postorder(root);
putchar('\n');
// system("pause");
}
return 0;
}
作者:火星十一郎
本文版权归作者火星十一郎所有,欢迎转载和商用,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.

浙公网安备 33010602011771号