POJ - 2255 B - Tree Recovery

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

题意:给出俩个字符串,分别表示一个二叉树的前序遍历和中序遍历,让你求这个二叉树的后序遍历。

分析:先序遍历的第一个节点一定是根节点,中序遍历的左边是左子树,右边是右子树,而后序遍历的最后一个是根节点,最前面的是左子树,中间的是右子树。假设位置k是中序遍历的根节点,那么后序遍历的0~k为左子树,k~n-2为右子树,最后一个为根节点。

代码:

#include<stdio.h>
#include<string.h>

void print(int n,char *s1,char *s2)  //节点数   前序遍历   中序遍历
{ 
    if(n<=0) return ;   //没有节点了  返回
    int p=strchr(s2,s1[0])-s2;    //p表示s1[0]在s2上第一次出现的位置
    print(p,s1+1,s2);          //节点s1{0]在s2前的所有节点  s1+1相当于把数组向前移动一位,因为s[0]已经不需要了
    print(n-p-1,s1+p+1,s2+p+1);//节点s1[0]在s2后的所有节点  只要树的右子树,s1+p+1相当于删除了左子树和根节点
    printf("%c",s1[0]);    //输出
}

int main()
{
    char str1[30],str2[30];
    while(~scanf("%s %s",str1,str2))
    {
        int l=strlen(str1);
        print(l,str1,str2);
        printf("\n");
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值