Tree Recovery POJ - 2255(根据先序序列和中序序列构建二叉树)

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

递归实现,有先序序列就知道了根结点,然后可以到中序序列中找左子树的中序序列(从头到根结点),也可以知道左子树有多少个结点,那么就可以从先序序列中读出左子树的先序序列。同理也可以获得右子树的先序和中序序列
OK,有了左右子树的先序序列和中序序列,那么就把问题拆小了。
解决小问题的方法是先序构建二叉树,那么就先构造根结点,然后左右孩子指针指向左右子树的跟结点,最小的情况是n==0,说明已经不是结点了,是叶子结点的左孩子或者右孩子。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct bint
{
    char data;
    struct bint* left;
    struct bint* right;
};
typedef struct bint* bintp;

bintp CTbyP_I(char* pre,char* in,int len);
void LRD(bintp root);
void del(bintp root);
int main()
{
    char preorder[27],inorder[27];
    while(scanf("%s%s",preorder,inorder) != EOF)
    {
        int len = strlen(preorder);
        bintp root = CTbyP_I(preorder,inorder,len);
        LRD(root);
        putchar('\n');
        del(root);
    }
}
bintp CTbyP_I(char* pre,char* in,int len)
{
    if(len == 0)
        return NULL;
    int mid = 0;
    for(;pre[0] != in[mid];mid++);
    bintp root = (bintp)malloc(sizeof(struct bint));
    root->data = pre[0];
    root->left = CTbyP_I(pre + 1,in,mid);
    root->right = CTbyP_I(pre + mid + 1,in + mid + 1,len - mid - 1);
    return root;
}
void LRD(bintp root)
{
    if(root == NULL)
        return;
    LRD(root->left);
    LRD(root->right);
    putchar(root->data);
}
void del(bintp root)
{
    if(root == NULL)
        return;
    del(root->left);
    del(root->right);
    free(root);
}
发布了39 篇原创文章 · 获赞 4 · 访问量 5747

猜你喜欢

转载自blog.csdn.net/weixin_45725137/article/details/105397030