编写递归算法,计算二叉树中叶子结点的数目。
二叉链表类型定义:
typedef struct BiTNode {
TElemType data;
BiTNode *lchild, *rchild;
} BiTNode, *BiTree;
实现函数如下:
void Leaves(BiTree bt, int &x)
/* Count the leaf node of the BiTree */
/* whose root node is bt to x. */
{
if(bt){
if(!bt -> lchild && !bt ->rchild){
++x;
}
else{
Leaves(bt -> lchild,x);
Leaves(bt -> rchild,x);
}
}
}