求二叉树中叶子结点的数目

来源:百度知道 编辑:UC知道 时间:2024/05/29 07:40:13
已知一个二叉树按照二叉链表方式存储,编写算法计算二叉树中叶子结点的数目

int LeafCount_BiTree(Bitree T)/*求二叉树中叶子结点的数目*/
{
if(!T) return 0; /*空树没有叶子*/
else if(!T->lchild&&!T->rchild) return 1; /*叶子结点*/
else return Leaf_Count(T->lchild)+Leaf_Count(T->rchild);/*左子树的叶子数加上右子树的叶子数*/
}/*LeafCount_BiTree */

编算法的话使用递归函数,传递每个顶点,到达一个叶子节点计数值加一,返回