#include
using namespace std;
typedef struct node
{
char data;
struct node *lchild,*rchild;
}BiTNode,*BiTree;
BiTree pre_mid_createBiTree(char *pre,char *mid,int len)
{
if(len0)
return NULL;
char ch=pre[0];//找到先序的第一个节点
int index=0;
while(mid[index]!=ch)//在中序中找到根结点的左边为该结点的左孩子
{
index++;
}
BiTree T=new BiTNode;//创建根结点
T->data=ch;
T->lchild=pre_mid_createBiTree(pre+1,mid,index);//创建左子树
T->rchild=pre_mid_createBiTree(pre+index+1,mid+index+1,len-index-1);//建立右子树
return T;
}
BiTree pro_mid_createBiTree(char *last,char *mid,int len)
{
if(len0)
return NULL;
char ch=last[len-1];//取得后序遍历顺序最后一个结点
int index=0;
while(mid[index]!=ch);
index++;
BiTree T=new BiTNode;//创建根结点
T->data=ch;
T->lchild=pro_mid_createBiTree(last,mid,index);//建立左子树
T->rchild=pro_mid_createBiTree(last+index,mid+index+1,len-index-1);
return T;
}