poj 2255 二叉树遍历

来源:互联网 发布:windows10的编程软件 编辑:程序博客网 时间:2024/05/18 00:24
Tree Recovery
Time Limit: 1000MS
Memory Limit: 65536KTotal Submissions: 11423
Accepted: 7169

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 ABCDEFGBCAD CBAD 


Sample Output


ACBFGEDCDAB


Source

Ulm Local 1997


题意:给出二叉树的前序遍历和中序遍历求后序
遍历

解题思路:

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

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

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

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

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

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


代码如下:

#include <stdio.h>#include <string.h>char post[30];int point=0;void right_to_left(char pre[],char inor[]){int i,j,k,flag,temp,len;char pre_temp[30],inor_temp[30];post[point++] = pre[0];//根len = strlen(inor);if(len == 1)return ;for(i=0;i<len;i++){if(inor[i] == pre[0])//在中序中找到树根所在的位置 { //左边是左子树,右边是右子树 temp = i;break;}}//printf("temp=%d\n",temp);//因为后序遍历数组post是倒着存储的,所以要先处理根,然后右子树然后左子树 //处理右子树 flag = 0;for(i=0,j=temp+1;j<len;j++) //提取右子树中序{inor_temp[i++]=inor[j];flag = 1;}if(flag)//当中序存在时提取前序 {inor_temp[i] = '\0';for(i=0,j=temp+1;j<len;j++)//提取右子树前序pre_temp[i++]=pre[j];pre_temp[i]='\0';right_to_left(pre_temp,inor_temp);}//处理左子树 flag = 0;for(i=0,j=0;j<temp;j++)//提取左子树中序{inor_temp[i++]=inor[j];flag = 1;}if(flag){inor_temp[i] = '\0';//提取左子树前序for(i=0,j=1;i<temp;j++)//j=1是因为pre[0]已经赋值给post[]了,已经处理过了,就不需要再处理了 pre_temp[i++]=pre[j];  //i<temp是因为以temp为界,左边字符的个数 pre_temp[i]='\0';right_to_left(pre_temp,inor_temp);}return ;}int main(){char pre[30],inor[30];while(scanf("%s%s",pre,inor)!=EOF){int i,j,k;//puts(pre);//puts(inor);memset(post,0,sizeof(post));right_to_left(pre,inor);for(--point;point>=0;point--)//倒序输出 {printf("%c",post[point]);}printf("\n");point = 0;}}//AC  0MS//给出二叉树的前序和中序求后序 

体会:第一次写树的遍历


附上改编的由二叉树的中序和后序求前序代码:(如有错误,欢迎指正)

#include <stdio.h>#include <string.h>char pre[30];int point=0;void right_to_left(char inor[],char post[]){    int i,j,k,flag,temp,len;    char inor_temp[30],post_temp[30];    len = strlen(inor);        pre[point++] = post[len-1];//根        if(len == 1)        return ;    for(i=0;i<len;i++)    {        if(inor[i] == post[len-1])//在中序中找到树根所在的位置        {                     //左边是左子树,右边是右子树            temp = i;            break;        }    }    //printf("temp=%d\n",temp);    //处理左子树    flag = 0;    for(i=0,j=0;j<temp;j++)//提取左子树中序    {        inor_temp[i++]=inor[j];        flag = 1;    }    if(flag)    {        inor_temp[i] = '\0';        //提取左子树后序        for(i=0,j=0;i<temp;j++)            post_temp[i++]=post[j];        post_temp[i]='\0';        right_to_left(inor_temp,post_temp);        }        //处理右子树    flag = 0;    for(i=0,j=temp+1;j<len;j++) //提取右子树中序    {        inor_temp[i++]=inor[j];        flag = 1;    }    if(flag)                //当中序存在时提取前序    {        inor_temp[i] = '\0';        for(i=0,j=temp;j<len-1;j++)//提取右子树后序            post_temp[i++]=post[j];        post_temp[i]='\0';        right_to_left(inor_temp,post_temp);        }        return ;}int main(){    char post[30],inor[30];    while(scanf("%s%s",inor,post)!=EOF)    {        int i,j,k;        //puts(pre);//puts(inor);        memset(pre,0,sizeof(pre));                right_to_left(inor,post);                for(i=0;i<point;i++)        {            printf("%c",pre[i]);        }        printf("\n");        point = 0;    }}//给出二叉树的中序和后序求前序


0 0
原创粉丝点击