编写一个程序,统计二叉树中叶子结点的个数。
void CountLeaf (BiTree T, int& count){
if ( T ) {
if ((!T->lchild)&& (!T->rchild))
count++; // 对叶子结点计数
CountLeaf( T->lchild, count);
CountLeaf( T->rchild, count);
} // if
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; }
int leaf(BiTree root) { int ans=0; if(root) ans=(root->LChild==NULL&&root->RChild==NULL); return ans+leaf(root->LChild)+leaf(root->RChild); }
这道题你会答吗?花几分钟告诉大家答案吧!
扫描二维码,关注牛客网
下载牛客APP,随时随地刷题