计算二叉树中叶子结点的数目

来源:互联网 发布:python win32api 64 编辑:程序博客网 时间:2024/04/16 16:13

编写递归算法,计算二叉树中叶子结点的数目。

二叉链表类型定义:

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);        }    }}

0 0