已知二叉树的中序遍历序列和后序遍历序列,还原二叉树,我的程序总是不对,求修改
上网找了很多资料,自认为理解了算法,可是总是输出一堆乱码,求修改啊,感激不尽,我快想疯了


程序代码:#include "stdio.h"
#include "malloc.h"
#include "string.h"
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
typedef char ElemType;
typedef struct BiTNode{
ElemType data;
struct BiTNode *lchild,*rchild;//左右孩子指针
} BiTNode,*BiTree;
BiTree PreCreate(char *post,char *in,int len)
{
int leftlen,rightlen;
char *p;
BiTree root;
if(len<=0)
return NULL;
root=(BiTNode *)malloc(sizeof(BiTNode));
root->data=post[len-1];
// printf("%c",root->data);
for(p=in;p-in<len;p++)
{
if(*p==root->data)
break;
}
leftlen=p-in;
rightlen=len-leftlen-1;
root->lchild=PreCreate(post+1,in,leftlen);
root->rchild=PreCreate(post+leftlen+1,p+1,rightlen);
return root;
}
void print(BiTree T) // 打印后序遍历序列
{
if(T==NULL) return;
print(T->lchild);
print(T->rchild);
printf("%c",T->data);
}
int main()
{
int len=6;
char post[6]={'d','e','b','f','c','a'},in[6]={'d','b','e','a','f','c'}; // 存储后序和中序遍历的序列
BiTree T;
T=(BiTNode *)malloc(sizeof(BiTNode));
T=PreCreate(post,in,len);
print(T);
printf("\n");
return 0;
}[ 本帖最后由 ksws0191053 于 2011-10-23 01:10 编辑 ]








