POJ 2255

简介: //由先中序建树,然后后序遍历 #include #include #include #include using namespace std; typedef struct Node { char data; Node *lchild,*rchild...
//由先中序建树,然后后序遍历 
#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;
}
        
        

 

目录
相关文章
|
人工智能 vr&ar
|
机器学习/深度学习
|
机器学习/深度学习