【二叉树代码】考试常考的代码题(求叶子数、非叶子数、度为1、度为2、深度…)

1、求度为0(叶子结点)的结点个数?***

//1.二叉树求叶子个数(度为0)
//算法思想:用递归,左右子树都是空就是叶子
typedef struct BTNode{
    int data;
    struct BTNode *lchild;
    struct BTNode *rchild;
}BTNode,*BiTree;
void leafcount(BiTree T,int &count){                     
    if(T){
        if((T->lchild==NULL)&&(T->rchild==NULL))
            count++;      
        else{   
            leafcount(T->lchild,count);
            leafcount(T->rchild,count);
        }  
    }
}


//2.用孩子兄弟链表求叶子个数
typedef struct HXNode{
    int data;
    struct HXNode *firstchild;
    struct HXNode *nextsibling;
}HXNode,*HXTree;
void leafcount(HxTree T,int *count){
    if(T){
        if(T->firstchild=NULL)
            *count++;        //没有孩子就是叶子
        leafcount(T->firstchild,count);
        leafcount(T->nextsibling,count);
    }
    else{return 0;}
}

2、求度为1的结点个数?*

//二叉树求度为1个数
//算法思想:用递归,01或10就是度为1
typedef struct BTNode{
    int data;
    struct BTNode *lchild;
    struct BTNode *rchild;
}BTNode,*BiTree;
void D1(BiTree T,int &count){
    if(T==NULL){return 0;}
    if((T->lchild==NULL&&T->rchild!=NULL)||(T->lchild!=NULL&&T->rchild==NULL)){
        return D1(T->lchild)+D1(T-rchild)+1;
    }
	else{
		return D1(T->lchild)+D1(T->rchild);
    }
}

3、求度为2的结点个数?**

//算法思想:求度为2,即左右孩子均不为空
typedef struct BTNode{
    int data;
    struct BTNode *lchild;
    struct BTNode *rchild;
}BTNode,*BiTree;
void D2(BiTree T,int &count){
    if(!T){return 0;}
    if(T->lchild!=NULL&&T->rchild!=NULL)
        return D2(T->lchild)+D2(T->rchild)+1;
    else{
        return D2(T->lchild)+D2(T->rchild);
    }
}

4、求叶子和非叶子结点个数?**

void count(BiTree T, int *n,*n0){
    if(T){
        if(T->lchild==NULL&&T->rchild==NULL){
            *n0++;
        }
        else{ n++;}   //除了叶子就是非叶子
        count(T->lchild,n,n0);
        count(T->rchild,n,n0);
    }
}

5、求二叉树结点数?*

int n;
void count(BTree *p){
    if(p){
        ++n;
        count(p->lchild);
        count(p->rchild);
    }
}

6、求二叉树深度?**

//1、二叉树求深度
int depth(BiTree T){
    if(!T)
        return 0;
    else{
        int ldeep=depth(T->lchild);
        int rdeep=depth(T->rchild);
        deep=1+(ldeep > rdeep ? ldeep : rdeep);
    }     //把根结点加上   
    return deep;
}


//2、孩子兄弟法求深度
typedef struct HXNode{
    int data;
    struct HXNode *firstchild;
    struct HXNode *nextsibling;
}HXNode,*HXTree;
int depth(HXTree T){
    if(!T)
        return 0;
    else{
        int ldeep=depth(T->firstchild)+1; //左孩子是真孩子
        int rdeep=depth(T->nextsibling);  //右孩子是兄弟
        if(ldeep>rdeep)
            return ldeep;
        else
            return rdeep;
    }
}

本篇全部使用递归,非递归的后面再写。

数据结构考试二叉树部分(孩子兄弟链表法)最爱考的就是这些,希望对您有用!一起进步!

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

极限的哥哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值