只给出函数
void countleaf(struct node *root)//统计叶子数;
{
if(root)
{
if((!root->lchild)&&(!root->rchild))
{
count++;
}
countleaf(root->lchild);
countleaf(root->rchild);
}
}
int max(int a, int b)//比较大小
{
if(a>b) return a;
else return b;
}
int hight(struct node *root)//从根到子结点最深的一条长度(根节点的深度为1)
{
if(root==NULL) return 0;
else return max(hight(root->lchild), hight(root->rchild))+1;
}
本文介绍了二叉树的叶子节点计数方法及计算二叉树高度的递归算法。通过两个实用函数实现:一是统计二叉树中叶子节点的数量;二是计算二叉树的高度,即从根节点到最远叶子节点的最长路径长度。
1069

被折叠的 条评论
为什么被折叠?



