BiTree PreInCreat(char Pre[],char In[],int L1,int R1,int L2,int R2)
{
//L1,R1是先序序列中的首尾指针
//L2,R2是中序序列中的首尾指针
BiTree root = (BiTree)malloc(sizeof(BTNode);为子树创建根节点
root->data = Pre[L1];//先序的第一个元素是一棵子树的根节点
for(i = L2;B[i]!=root->data;i++)//在中序序列中寻找根节点位置
{
llen = i-L2;左子树节点个数
rlen = R2-i; 右子树节点个数
}
if(llen)//左子树中个数不为零
root->lchild = PreInCreat(Pre,In,L1+1,L1+llen,L2,L2+len-1);//自己手动画图推导
else
root->lchild=NULL;
if(rlen)
root->rchild = PreInCreat(Pre,In,R1-rlen+1,R1,R2-rlen+1,R2);
else
root->rchild=NULL;
return root;//返回根节点
}
由先序和中序构建二叉树
于 2024-10-03 20:31:26 首次发布