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;

}

        

        

 

你可能感兴趣的:(poj)