UVA 536 Tree Recovery(由前序中序求后序,经典)



 Tree Recovery

Time Limit: 3000 MS

Little Valentine liked playing with binary trees very much. Her favorite game was constructing randomly looking binary trees with capital letters in the nodes.

This is an example of one of her creations:

                                    D
                                   / \
                                  /   \
                                 B     E
                                / \     \
                               /   \     \ 
                              A     C     G
                                         /
                                        /
                                       F

To record her trees for future generations, she wrote down two strings for each tree: a preorder traversal (root, left subtree, right subtree) and an inorder traversal (left subtree, root, right subtree).

For the tree drawn above the preorder traversal is DBACEGF and the inorder traversal is ABCDEFG.

She thought that such a pair of strings would give enough information to reconstruct the tree later (but she never tried it).


Now, years later, looking again at the strings, she realized that reconstructing the trees was indeed possible, but only because she never had used the same letter twice in the same tree.

However, doing the reconstruction by hand, soon turned out to be tedious.

So now she asks you to write a program that does the job for her!

Input Specification 

The input file will contain one or more test cases. Each test case consists of one line containing two strings preord and inord, representing the preorder traversal and inorder traversal of a binary tree. Both strings consist of unique capital letters. (Thus they are not longer than 26 characters.)

Input is terminated by end of file.

Output Specification 

For each test case, recover Valentine's binary tree and print one line containing the tree's postorder traversal (left subtree, right subtree, root).

Sample Input 

DBACEGF ABCDEFG
BCAD CBAD

Sample Output 

ACBFGED
CDAB

题目大意:

给你一棵二叉树的前序和中序,求后序。

解题思路:

递归呀。


代码一:

#include<iostream>
#include<string>
#include<cstdio>
using namespace std;

const int maxN = 30;
struct Node{
    char ch;
    int l,r;
    Node(int l0=0,int r0=0){l=l0,r=r0;}
}tree[maxN];
string pre,in,post;
int index;

void build(string preOrder,string inOrder){
    int temp=index;
    tree[temp]=Node(-1,-1);
    tree[temp].ch=preOrder[0];
    //if(preOrder.length()==1) return ;

    for(int i=0;i<preOrder.length();i++){
        if(inOrder[i]==preOrder[0]){
            if(i>0){//如果有lson
                string lpre=preOrder.substr(1,i);
                string lin=inOrder.substr(0,i);
                tree[temp].l=++index;
                build(lpre,lin);
            }
            if(i<preOrder.length()-1){//如果有rson
                string rpre=preOrder.substr(i+1);
                string rin=inOrder.substr(i+1);
                tree[temp].r=++index;
                build(rpre,rin);
            }
        }
    }
}

void solve(){
    post.clear();
    index=0;
    build(pre,in);
}

void postMake(int cnt){
    if(tree[cnt].l!=-1) postMake(tree[cnt].l);
    if(tree[cnt].r!=-1) postMake(tree[cnt].r);
    post.push_back(tree[cnt].ch);
}

void output(){
    cout<<post<<endl;
}

int main(){
    while(cin >> pre >> in){
        solve();
        postMake(0);
        output();
    }
    return 0;
}

代码二:(老师写的赞赞的代码,耶)

/*
UVa536 Tree Recovery
Coded by Guojin Zhu
Run Time 0.008s
AC on November 2,2012
*/
#include<iostream>
#include<string>
using namespace std;
///
class TreeRecovery{
private:
    string preord;
    string inord;
    string postorder;
    void reconstruct(string p, string i);
public:
    void initial(string p, string i){
        preord = p;
        inord = i;
        postorder.clear();
    }
    void computing(){reconstruct(preord, inord);}
    void outResult(){cout << postorder << endl;}
};
void TreeRecovery::reconstruct(string preord, string inord){
    int sz = preord.size();
    if(sz > 0){
        int p = int(inord.find(preord[0]));
        reconstruct(preord.substr(1, p), inord.substr(0, p)); //left subtree
        reconstruct(preord.substr(p+1, sz-p-1), inord.substr(p+1, sz-p-1)); //right subtree
        postorder.push_back(preord[0]); //root
    }
}
/
int main(){
    TreeRecovery tr;
    string p, i;
    while(cin >> p >> i){
        tr.initial(p, i);
        tr.computing();
        tr.outResult();
    }
    return 0;
}
/*
INPUT
DBACEGF ABCDEFG
BCAD CBAD
AB BA
--------------
OUTPUT
ACBFGED
CDAB
BA
*/


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值