Tree recovery

                                                                                                                   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

                      题目大意:题目意思很明显,就是给你前序遍历和中序遍历的一列数组,要你求出后续遍历的数组

                      解决方法:

                                      第一步:一般思路给你前序遍历和中序遍历,只要找到前序遍历的root在中序遍历的位置,那么就可以将其分成两半,左子树和右子树

                                                       每次都建立的是前序遍历的根,继续遍历左,遍历右;

                                      第二步:利用递归的方法创建二叉树;

                                      第三步:用后序遍历打印出二叉树

                    源代码(源代码均为别人所写):

#include<iostream>
#include<string>
#include<malloc.h>
#include<cstdio>
using namespace std;


typedef struct node
{
    char data;
    node *lc, *rc;
}BiTree, *BiNode;
BiNode pre_mid_ct(string pre, string mid)
{
     BiNode root = NULL;
     if(pre.length()>0)
     {
         root = (BiNode)malloc(sizeof(BiTree));
         int k;
         root->data = pre[0];
         k = mid.find(root->data);
         root->lc = pre_mid_ct(pre.substr(1, k), mid.substr(0, k));
         root->rc = pre_mid_ct(pre.substr(k+1), mid.substr(k+1));
    }
    return root;
}
void PostOrder(BiNode head)
{
    if(head)
    {
        PostOrder(head->lc);
        PostOrder(head->rc);
        printf("%c", head->data);
    }
}
int main()
{
    string pre , mid;
    while(cin>>pre>>mid)
    {
        BiNode T = NULL;
        T = (BiNode)malloc(sizeof(BiTree));
        T = pre_mid_ct(pre, mid);
        PostOrder(T);
        printf("\n");
    }
    return 0;
}




 

wangxiaoming CSDN认证博客专家 架构 Spring Boot Redis
博客是很好的总结和记录工具,如果有问题,来不及回复,关注微信公众号:程序员开发者社区,获取我的联系方式,向我提问,也可以给我发送邮件,联系 1275801617@qq.com
©️2020 CSDN 皮肤主题: Age of Ai 设计师:meimeiellie 返回首页
实付 19.89元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值