4.以二叉链表作为二叉树的存储结构, 编写算法计算给定二叉树中叶子结点总数
。
```c++
// 以二叉链表作为二叉树的存储结构, 编写算法计算给定二叉树中叶子结点总数
// 二叉链表的定义
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);
}
}
```
免责声明:
以上内容除特别注明外均来源于网友提问,创作工场回答,未经许可,严谨转载。