POJ 2255

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//由先中序建树,然后后序遍历
#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;
}
         
        

 

posted @   加拿大小哥哥  阅读(248)  评论(2编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示