首页 > 代码库 > POJ 2255 Tree Recovery

POJ 2255 Tree Recovery

提示:二叉树遍历。给出前序和中序,求后序。

解题思路

 

1、前序遍历的第一个字母必是 根

2、在中序遍历的字母串中找出 根字母,那么根字母左右两边的字符串就分别是它的左、右子树

3、利用递归复原二叉树(把子树看作新的二叉树)

4、后序遍历特征:后序遍历字母串 自右至左 依次为:

最外层(总树,设为第0层)右子树的根,内1层右子树的根,内2层右子树的根….内n层右子树的根,内n层左子树的根,内n-1层左子树的根……内1层左子树的根,最外层(总树,第0层)左子树的根。把总树的左子树作为新的总树,继续递归即可。   (注意:总树的叶就是作为“单叶”这棵树本身的右根)

5、输出后序遍历时,只需按4的顺序从左到右排列,再倒置输出即可


Tree Recovery
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 11358 Accepted: 7121

Description

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

The input 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

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

Source

Ulm Local 1997



#include <iostream>
#include <cstring>
#define maxn 30
using namespace std;
char q[maxn],z[maxn];
void dfs(int qs,int qw,int zs,int zw)
{
        int leftsize=0,rightsize=0;//左子树和右子树的规模

        int root;
        for(root=zs;root<=zw;root++)
        {
            if(q[qs]==z[root])
            {
                leftsize=root-zs;
                rightsize=zw-root;
                break;
            }
        }

        if(leftsize>0)
            dfs(qs+1,qw+leftsize,zs,root-1);
        if(rightsize>0)
            dfs(qs+leftsize+1,qw,root+1,zw);

        cout<<z[root];
}
int main()
{
    while(cin>>q>>z)
    {
       int n=strlen(q);//计算节点数
       dfs(0,n-1,0,n-1);
       cout<<endl;
    }
    return 0;