POJ2255 Tree Recovery 두 갈래 나무 범람

Tree Recovery
Time Limit: 1000MS
 
Memory Limit: 65536K
Total Submissions: 12523
 
Accepted: 7836
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

전순과 중순에서 후순을 구하는 것은 비교적 이해하기 쉽다고 할 수 있다. HDU1710과 마찬가지로 구체적으로 그 블로그를 참고하자. 훑어보는 인터넷에 대한 검색도 한 무더기다.
HDU1710
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;

char pre[30],mid[30],n;

typedef struct Node{
    char v;
    struct Node *left,*right;
}node;

node* root;

node* newNode(char x){
    node* u=(node*)malloc(sizeof(node));
    u->left=u->right=NULL;
    u->v=x;
    return u;
}

node* creat(char *pre,int l1,char *mid,int l2){
    int i,j,k;
    for(i=0;i<l2;i++)
        if(pre[0]==mid[i]) break;
    node* u=newNode(mid[i]);
    if(i>0)
        u->left=creat(pre+1,i,mid,i);
    if(i<l2-1)
        u->right=creat(pre+i+1,l2-i-1,mid+i+1,l2-i-1);
    return u;
}

void remove_tree(node* u){
    if(u==NULL) return ;
    remove_tree(u->left);
    remove_tree(u->right);
    delete u;
}

void print(node* u){
    if(u==NULL) return ;
    print(u->left);
    print(u->right);
    printf("%c",u->v);
}

int main()
{
    int i,j,n;
    while(scanf("%s",pre)!=EOF){
        scanf("%s",mid);
        n=strlen(pre);
        remove_tree(root);
        root=creat(pre,n,mid,n);
        print(root);
        printf("
"); } return 0; }

공간을 방출하는 좋은 습관을 기르다.

좋은 웹페이지 즐겨찾기