POJ2255Tree Recovery(二叉樹)

bigbigship發表於2014-07-25
Tree Recovery
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 11241   Accepted: 7048

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
題目大意:告訴你一個二叉樹的前序遍歷,中序遍歷,求它的後序遍歷
基礎知識:前序遍歷 根左右,中序遍歷 左根右, 後序遍歷 左右根
前序遍歷的第一個 後序遍歷的最後一個一定為根
因此我們從前序遍歷入手 在中序遍歷中找到跟的位置 然後將跟拿走 剩下 左子樹和右子樹 剩下的依然是兩個樹 繼續進行遞迴 然後得到整個的後續遍歷
例如
前序DBACEGF S1 中序ABCDEFG S2
第一個跟為D 在s2中找到D 剔除 剩下 ABC EFG 分別為左右子樹
然後再左子樹中找到B 左子樹中存在 左子樹中剩下 A,C分別為左右子樹 遞迴完畢
在右子樹中進行遞迴 首先在s1中找到 E 然後剩下FG 都在E的右子樹上 然後S1中搜到 G 剩下F,參照S2可知 為G的左兒子。
程式碼如下:
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;


const int N = 30;


char pre[N],mid[N];


int n;


void make_tree(int l,int r)
{
    int k;
    if(l>r)
        return;
    for(k=l;k<=r;k++)
        if(pre[n]==mid[k])
           break;
    n++;
    make_tree(l,k-1);//建左子樹:順序不可顛倒
    make_tree(k+1,r);//建右子樹;
    cout<<mid[k];
}


int main()
{
    while(cin>>pre>>mid){
        n=0;
        int len=strlen(pre)-1;
        make_tree(0,len);
        puts("");
    }
    return 0;
}

相關文章