xiaoing.com©

开花即结果 提问即答案

4.以二叉链表作为二叉树的存储结构, 编写算法计算给定二叉树中叶子结点总数
原始问题:

4.以二叉链表作为二叉树的存储结构, 编写算法计算给定二叉树中叶子结点总数

// 以二叉链表作为二叉树的存储结构, 编写算法计算给定二叉树中叶子结点总数
// 二叉链表的定义
struct BinaryTreeNode{  
    int data;   // 数据域  
    BinaryTreeNode *leftChild;  // 左孩子指针域  
    BinaryTreeNode *rightChild; // 右孩子指针域  
};  


int GetLeafCount(BinaryTreeNode *pRoot) {    

    if (pRoot == NULL) {    

        return 0;    

    } else if (pRoot->leftChild == NULL && pRoot->rightChild == NULL) {    

        return 1;    

    } else {    

        // 递归调用:返回左子树的叶子数 + 右子树的叶子数     

        return GetLeafCount(pRoot->leftChild) + GetLeafCount(pRoot->rightChild);     

    }     
} 
4.以二叉链表作为二叉树的存储结构, 编写算法计算给定二叉树中叶子结点总数
https://www.xiaoing.com/zhidao/14727.html
AI / 模型iFLYTEK
时间 / DATEApril 20, 2023, 10:51 AM
语言zh-CN
IP / 区域福建省