采用中序遍历,统计二叉树节点树int LeafCount=0;//全局变量,用于计算存储叶子节点数量(c语言)

设计算法,采用中根(中序)遍历统计二叉树中叶子结点数目。int LeafCount=0;//全局变量,用于计算存储叶子节点数量void leaf (BiTree root){//实现该方法,统计二叉树叶子节点个数。}

void leaf(BiTree root) {
    if (root == NULL) {
        return;
    }

    // 遍历左子树
    leaf(root->left);

    // 遍历右子树
    leaf(root->right);

    // 如果当前结点是叶子结点,则 LeafCount 加 1
    if (root->left == NULL && root->right == NULL) {
        LeafCount++;
    }
}

int LeafCount=0;
void LeafNumber(BiTree root)
{
    if(root!=NULL)
    {       
            LeafNumber(root->lchild);
            if(root->lchild==NULL&&root->rchild==NULL)
                LeafCount++;
            LeafNumber(root->rchild);
    }
}