//以下是我从网上下的程序,但是有些不足之处,希望大家帮我改正。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct BiNode{
char data;
BiNode *lchild,*rchild;
}*BiTree;
BiTree restore(char *ppos,char *ipos,int n)
{
BiTree ptr;
char *rpos;
int k;
if(n<=0)
return NULL;
ptr=(BiTree)malloc(sizeof(BiNode));
ptr->data=*ppos;
for(rpos=ipos;rpos<ipos+n;rpos++)
if(*rpos==*ppos)
break;
k=rpos-ipos;
ptr->lchild=restore(ppos+1,ipos,k);
ptr->rchild=restore(ppos+k+1,rpos+1,n-k-1);
return ptr;
}
void postorder(BiTree ptr)
{
if(ptr==NULL)
return;
else
{
postorder(ptr->lchild);
printf("%c\t",ptr->data);
postorder(ptr->rchild);
}
}
void main()
{
BiTree root;
char inod[6]={'4','2','5','1','3','6'};//中序
char pred[6]={'1','2','4','5','3','6'};//前序
root=restore(pred,inod,strlen(pred));
postorder(root);
}