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

题意描述:

就是给出二叉树的先序遍历和中序遍历,让求后序遍历。

解题思路:

先说一下先,中,后序遍历二叉树是怎么建立的
先序遍历二叉树的操作定义如下:
若二叉树为空,则空操作;否则
(I)访问根结点;
(2)先序遍历左子树;
(3)先序遍历右子树。
中序遍历二叉树的操作定义如下:
若二叉树为空,则空操作;否则
(I)中序遍历左子树;
(2)访问根结点;
(3)中序遍历右子树。
后序遍历二叉树的操作定义如下:
若二叉树为空,则空操作;否则
(I)后序遍历左子树;
(2)后序遍历右子树;
(3)访问根结点。
先序遍历:先访问根节点,然后以前序访问左子树,右子树。
中序遍历:左子树,当前节点,右子树。

根据先序和中序遍历的特点,就可以发现如下规律:
先序遍历的每个节点,都是当前子树的根节点。同时,以对应的节点为边界,就会把中序遍历的结果分为左子树和右子树。

样例分析:

Sample Input
DBACEGF ABCDEFG
Sample Output
ACBFGED

先序:DBACEGF
'D’是根节点
在后序遍历中根结点是在最后面的(ACBFGED
中序:ABCDEFG
'D’是根节点,把字符串分成左右两个子树
'D’是先序遍历节点的第一个元素,可以看出,它把中序遍历的结果分成’ABC’和’EFG’两部分,分别是左子树(ABC)和右子树(EFG)。
中序:ABCDEFG
找到先序遍历中对应的左子树(BAC)和右子树(EGF)。
先序:DBAC EGF
就可以把’D’作为当前的根节点,然后依次递归下去,这样就能够依次恢复左子树和右子树的遍历结果。

图解
把每棵树都当成最简单的二叉树

通过先序和中序遍历的特点找到根结点并且记录s3[n-1+ss1] = s1[ss];(因为是求后序遍历根结点是在当前二叉树最后面的)
在这里插入图片描述

把根结点记录后再把左右子树分别当成最简单的二叉树
kk(i, ss+1, n1, ss1);//左子树
kk(n-i-1, ss+i+1, n1+i+1, ss1+i);//右子树

在这里插入图片描述

总结:

先找到根结点再找到左右子树再把左右子树,最后通过递归依次实现
(换一句话说就是一直查找根结点)

代码:
数组实现

#include <stdio.h>
#include <string.h>
char s1[30],s2[30],s3[30];

void kk(int l, int ss, int l1, int ss1)
{
    
    
	if(!l) //无子树
	return; 
	
	int i;
	for(i = 0; ; i++)
	{
    
    
		if(s1[ss] == s2[l1+i])//找到根结点 
		break;
	} 
	s3[l-1+ss1] = s1[ss];//记录后序遍历结果 
	kk(i, ss+1, l1, ss1);//左子树 
	kk(l-i-1, ss+i+1, l1+i+1, ss1+i);//右子树 
	
}
int main()
{
    
    
	int n;
	while(scanf("%s%s", s1, s2) != EOF)
	{
    
    
		n= strlen(s1);
		kk(n, 0, 0, 0);
		for(int i=0;i<n;i++)
		printf("%c",s3[i]);
		printf("\n");
	}
	return 0;
} 

链表实现

#include <stdio.h>
#include <string.h>
#include <malloc.h>
typedef struct node
{
    
    
    char ch;
    struct node *left, *right;
}Node;         //定义节点的结构
 
node *creat(char *p, char *p1, int len)
{
    
    
    int k;
    if (!len)//出口//无子树
        return NULL;
    Node *head= (Node*)malloc(sizeof(Node));
    head -> ch = *p;
    char *p2;
    for (p2 = p1; p2 != NULL; p2++){
    
    
        if (*p2 == *p)    //找到根结点 
            break;
    }
    k = p2 - p1;
    head -> left = creat(p + 1, p1, k);    //左子树 
    head -> right = creat(p + k + 1, p2 + 1, len - k - 1);    //右子树
    return head;
}
 
void print(Node *head)    //输出序列
{
    
    
    if (head == NULL)
        return ;
    print(head -> left);
    print(head -> right);
    printf("%c",head -> ch);
}
 
int main()
{
    
    
    char p[30], p1[30];      //存储先序和中序遍历的序列
    Node *head;
    head = (Node*)malloc(sizeof(Node));
    while (scanf("%s %s",p,p1)!=EOF)
	{
    
    
        int len = strlen(p);
        head = creat(p, p1, len);
        print(head);
       printf("\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_46703995/article/details/112465533