POJ 2255 Tree Recovery (二叉树)

Tree Recovery
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8946   Accepted: 5647

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

1,递归法:

在C语言中 strchr 和 strstr函数都被包含在<string.h>头文件中,也就是要调用它们时要在程序前面包含<string.h>头文件,也就是写这个语句:
#include<string.h>,strchr函数原型:char * strchr(char * str, int ch);
功能就是找出在字符串str中第一次出项字符ch的位置,找到就返回该字符位置的指针(也就是返回该字符在字符串中的地址的位置),找不到就返回空指针(就是 null)。 strstr 函数原型: char * strstr(char * str1,char * str2);功能就是找出在字符串str1中第一次出项字符串str2的位置(也就是说字符串sr1中要包含有字符串str2),
找到就返回该字符串位置的指针(也就是返回字符串str2在字符串str1中的地址的位置),找不到就返回空指针(就是 null)。
#include<stdio.h>
#include<string.h>

char s1[30],s2[30];

void build(int len,char *s1,char *s2){
    if(len<=0)
        return ;
    int loc=strchr(s2,s1[0])-s2;
    build(loc,s1+1,s2);
    build(len-loc-1,s1+loc+1,s2+loc+1);
    printf("%c",s1[0]);
}

int main(){

    //freopen("input.txt","r",stdin);

    int len;
    while(scanf("%s%s",s1,s2)!=EOF){
        len=strlen(s1);
        build(len,s1,s2);
        printf("\n");
    }
    return 0;
}

二:非递归法

#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>

using namespace std;

string s1,s2;

struct node{
    int pt;
    char x;
    struct node *left;
    struct node *right;
};

void postorder(node *root){
    if(root==NULL)
        return ;
    postorder(root->left);
    postorder(root->right);
    cout<<root->x;
}

int main(){

    //freopen("input.txt","r",stdin);

    int i,j;
    while(cin>>s1>>s2){
        node *root=new(node);
        root->left=NULL;
        root->right=NULL;
        root->x=s1[0];
        root->pt=0;
        for(j=0;j<(int)s2.length();j++)
            if(s1[0]==s2[j])
                break;
        root->pt=j;

        for(i=1;i<(int)s1.length();i++){
            for(j=0;j<(int)s2.length();j++)
                if(s1[i]==s2[j])
                    break;
            node *p=root,*pre=NULL;
            while(p){
                pre=p;
                if(j<pre->pt)
                    p=p->left;
                else
                    p=p->right;
            }
            node *t=new(node);
            t->left=NULL;
            t->right=NULL;
            t->x=s1[i];
            t->pt=j;
            if(j<pre->pt)
                pre->left=t;
            else
                pre->right=t;
        }
        postorder(root);
        cout<<endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/jackge/p/2971067.html