6-1 統計二叉樹結點個數 (10 分)
本題要求實現一個函數,可統計二叉樹的結點個數。
函數接口定義:
int NodeCount ( BiTree T);
T是二叉樹樹根指針,函數NodeCount返回二叉樹中結點個數,若樹為空,返回0。
裁判測試程序樣例:
#include <stdio.h> #include <stdlib.h> typedef char ElemType; typedef struct BiTNode { ElemType data; struct BiTNode *lchild,*rchild; }BiTNode,*BiTree; BiTree Create();/* 細節在此不表 */ int NodeCount ( BiTree T); int main() { BiTree T = Create(); printf("%d\n", NodeCount(T)); return 0; } /* 你的代碼將被嵌在這里 */
輸出樣例(對於圖中給出的樹):
6
1 int NodeCount(BiTree t){ 2 if(t==NULL) 3 return 0; 4 else 5 return NodeCount(t->lchild) + NodeCount(t->rchild) + 1; 6 }