前序中序/后序中序确定二叉树

这个代码网上不要太多了,考研复习,贴出比较容易理解的代码,但是写得丑(只是写了比较完整的思路,你看我类型定义都没定义呢)。。。
前序、中序确定二叉树

void preInCreat(Tree  r,ElemType a[],ElemType b[],int as,int ad,int bs,int bd)
{
    //as:A数组第一个元素的下标,as:A数组最后一个元素下标,bs,as,bs初值为1
    if(as>ad)
        return;
    r=(treeNode*)malloc(sizeof(treeNode));
    r->data=a[as];
    int i;
    for(i=bs;b[i]!=r->data;i++)
    int llenth=i-bs;//左子树长度
    int rlenth=bd-i;//右子树长度
    if(llenth)
        preInCreat(r->lchild,a,b,as+1,as+llenth,bs,bs+llen-1);
    else
        r->lchild=NULL;
    if(rlenth)
        preInCreat(r->rchild,a,b,ad-rlen+1,ad,bd-rlenth+1,bd);//关于下标问题一定要画画图搞搞清楚
    else
        r->rchild=NULL;
}

后序、中序确定二叉树

void postInCreat(Tree r,ElemType a[],ElemType b[],int as,int ad,int bs,int bd)
{
    if(as>ad)
        return;
    r=(treeNode*)malloc(sizeof(treeNode));
    r->data=a[ad];//后序遍历根结点是最后一个元素
    int i;
    for(i=bs;b[i]!=r->data;i++)
    int llenth=i-bs;
    int rlenth=bd-i;
    if(llenth)
        postInOrder(r->lchild,a,b,as,as+llenth-1,bs,bs+llenth-1);
    else
        r->lchild=NULL;
    if(rlenth)
        postInOrder(r->rchild,a,b,ad-rlenth,ad-1,bd-r+1,bd);
    else
        r->rchild=NULL;
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值