Tree Recovery 字典树 (Ulm Local 1997)

Tree Recovery
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 12530 Accepted: 7840

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

题意:输入两组数据,分别是前序遍历序列和中序遍历序列,你需要编写程序通过这两组数据求出该树的后序遍历序列(前序序列 + 中序序列 = 后序序列)

本题考察的是递归+树的遍历方式,在此我们温故一下三种遍历方式,前序遍历,中序遍历,后序遍历。三者有密切联系,其中的两者即可推出第三种序列。要善于发现其中的规律,前序都是从根节点开始遍历,我们顺着根向下走,每一个父节点的子节点都可以看作父节点,即具有递归结构不变性。

假设 前序序列是 A B E H F C G I

 中序序列是 H E B F A C I G (图如下)


每一次从前序序列里,按顺序抽取出字母就能将中序序列分割,并根据中序遍历的特性。分割后的两部分分别是 左子树 和 右子树(注意,他们也是二叉树!)

就像这样:取A, 中序序列被分割为 左子树:H E B F  右子树 C I G

继续取B,但是这次是对左子树:H E B F 进行分割。 分割结果是: 左子树:H E  右子树  B F

直到不能再分割,递归会返回去到第一次使用 A 分割出来的 右子树 里继续分割

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

 using namespace std;
typedef long long LL;
const int maxN = 50;

char pre[maxN], mid[maxN];
void recover(int preleft, int preright, int midleft, int midright)
 {
    int root, leftsize, rightsize;

     //在中序中查找根节点
    for (root = inleft; root <= inright; root++)
      if (pre[preleft] == mid[root]) break;

     //计算子树的大小
    leftsize = root - midleft;
     rightsize = midright - root;

        //递归子树
     if(leftsize > 0)  recover(preleft + 1, preleft + leftsize, midleft, root - 1);
    if(rightsize > 0) recover(preleft + leftsize + 1, preright, root + 1,midright);
    

     printf("%c", mid[root]);
 }

 void solve()
 {
    int len = strlen(pre);
     recover(0, len - 1, 0, len - 1);
     printf("\n");
 }
 
 int main()
 {
   
     while (~scanf("%s%s", pre, mid))solve();
     return 0;
 }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值