根据二叉树的前序序列和中序序列或者后序序列和中序序列均可唯一确定一个二叉树,下面为程序实现:
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#define MAX 10
typedef struct Tree{
int data;
struct Tree *left;
struct Tree *right;
}*BiTree, BiNode;
typedef struct list{
int data[MAX];
int size;
}Sqlist;
//重建二叉树
BiTree reConstructBinaryTree(Sqlist *pre, Sqlist *in){
if (in->size == 0){
return NULL;
}
BiTree b;
b = NULL;
if (pre->size!=0){
b = (BiTree)malloc(sizeof(BiNode));
b->data = pre->data[0];//前序序列的第一个为根结点
}
int i = 0;
int j = 0;
int father = 0;
for (i = 0; i < in->size ; i++){
if (in->data[i] == pre->data[0]){
father = i;//找出根结点位于中序序列的位置,可找出根结点的左右子树集合
break;
}
}
Sqlist pre_left, in_left, pre_right,in_right;//存放根结点的左子树和右子树的前序序列值集合和中序序列值集合
pre_left.size = 0;
in_left.size = 0;
pre_right.size = 0;
in_right.size