首页 > 学院 > 开发设计 > 正文

求二叉树的叶子个数以及深度

2019-11-08 01:13:37
字体:
来源:转载
供稿:网友

叶子个数

int num(struct BiTNode *t){    if (t != NULL)    {        if (t -> lchild == NULL && t -> rchild == NULL)            count ++;        else        {            num(t -> lchild);            num(t -> rchild);        }    }    return count;}

二叉树的深度

int depth(struct BiTNode *t)   {   int d1, d2;   if (t != NULL)   {   d1 = depth(t -> lchild);   d2 = depth(t -> rchild);   if (d1 > d2)     return d1 + 1;    else    return d2 + 1;    }   }


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表