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