6.42.c

来自「部分高校使用anyview编程测试数据结构习题,此代码为数据结构题集(c语言版)」· C语言 代码 · 共 26 行

C
26
字号
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;
} BiTNode, *BiTree;
void Leaves(BiTree bt, int &x)
/* Count the leaf node of the BiTree */
/* whose root node is bt to x.       */
{   int x0,x1;
    if(!bt) x=0;
    else if(!bt->rchild&&!bt->lchild) x=1;
    else {
          Leaves(bt->rchild,x0);
          Leaves(bt->lchild,x1);
          x=x0+x1;
           }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?