1-2 統計二叉樹葉子結點個數 (10 分)
阿新 • • 發佈:2018-12-11
本題要求實現一個函式,可統計二叉樹的結點個數。
函式介面定義:
int LeafCount ( BiTree T);
T是二叉樹樹根指標,函式LeafCount返回二叉樹中葉子結點個數,若樹為空,則返回0。
裁判測試程式樣例:
#include <stdio.h> #include <stdlib.h> typedef char ElemType; typedef struct BiTNode { ElemType data; struct BiTNode *lchild,*rchild; }BiTNode,*BiTree; BiTree Create();/* 細節在此不表 */ int LeafCount ( BiTree T); int main() { BiTree T = Create(); printf("%d\n", LeafCount(T)); return 0; } /* 你的程式碼將被嵌在這裡 */
輸出樣例(對於圖中給出的樹):
3
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;
}