首页 > 试题广场 >

编写一个程序,统计二叉树中叶子结点的个数。

[问答题]

编写一个程序,统计二叉树中叶子结点的个数。

void CountLeaf (BiTree T,  int& count){

if ( T ) {

if ((!T->lchild)&& (!T->rchild))

count++;     // 对叶子结点计数

CountLeaf( T->lchild, count);

CountLeaf( T->rchild, count);

} // if

} // CountLeaf
发表于 2017-09-21 15:36:25 回复(0)
更多回答
int LeafCount(BiTree T)
{
    int count;
    if(T==NULL)
        count=0;
    else if(T->lchild==NULL&&T->rchild==NULL)
        count=1;
    else
        count=LeafCount(T->lchild)+LeafCount(T->rchild);
    return count;
}

发表于 2019-07-31 00:05:45 回复(0)
int leaf(BiTree root)
{
    int ans=0;
    if(root)
        ans=(root->LChild==NULL&&root->RChild==NULL);
    return ans+leaf(root->LChild)+leaf(root->RChild);
}
编辑于 2019-12-14 21:09:41 回复(0)