二叉树节点个数、叶子节点个数、第k层节点个数和查找值为k的节点
在上一篇文章二叉树链式存储详解中,我们详细介绍了如何使用C语言实现二叉树的链式存储,包括二叉树节点的定义、创建节点、插入节点以及三种常见的遍历方式。在本文中,我们将继续探讨二叉树的一些常见操作,包括计算二叉树的节点个数、叶子节点个数、第k层节点个数以及查找值为k的节点。
计算二叉树的节点个数
计算二叉树的节点个数是一个常见的操作,可以使用递归的方式来实现。我们可以定义一个函数countNodes
,它接受一个指向二叉树根节点的指针作为参数,然后递归地计算左子树和右子树的节点个数,最后将它们相加并加上根节点本身的个数(1)。
int countNodes(TreeNode* root) {
if (root == NULL) {
return 0;
}
int leftCount = countNodes(root->left);
int rightCount = countNodes(root->right);
return leftCount + rightCount + 1;
}
在上面的代码中,countNodes
函数首先判断根节点是否为NULL
,如果是,则说明当前子树为空,返回0。否则,函数递归地计算左子树的节点个数leftCount
和右子树的节点个数rightCount
,最后返回leftCount + rightCount + 1
,即左子树节点个数、右子树节点个数和根节点本身的个数之和。
计算二叉树的叶子节点个数
叶子节点是指没有子节点的节点。计算二叉树的叶子节点个数也可以使用递归的方式来实现。我们可以定义一个函数countLeafNodes
,它接受一个指向二叉树根节点的指针作为参数,然后递归地判断当前节点是否为叶子节点,如果是,则返回1,否则递归地计算左子树和右子树的叶子节点个数之和。
int countLeafNodes(TreeNode* root) {
if (root == NULL) {
return 0;
}
if (root->left == NULL && root->right == NULL) {
return 1;
}
int leftLeafCount = countLeafNodes(root->left);
int rightLeafCount = countLeafNodes(root->right);
return leftLeafCount + rightLeafCount;
}
在上面的代码中,countLeafNodes
函数首先判断根节点是否为NULL
,如果是,则说明当前子树为空,返回0。然后,函数判断当前节点是否为叶子节点,即左子节点和右子节点都为NULL
,如果是,则返回1。否则,函数递归地计算左子树的叶子节点个数leftLeafCount
和右子树的叶子节点个数rightLeafCount
,最后返回它们的和。
计算二叉树第k层的节点个数
计算二叉树第k层的节点个数可以使用递归的方式来实现。我们可以定义一个函数countNodesAtLevel
,它接受三个参数:指向二叉树根节点的指针root
、表示目标层数的整数k
以及表示当前层数的整数level
(初始值为1,表示根节点所在的层)。
函数首先判断根节点是否为NULL
,如果是,则返回0。然后,判断当前层数level
是否等于目标层数k
,如果是,则返回1,表示找到了一个第k层的节点。否则,函数递归地计算左子树和右子树中第k层的节点个数,并将它们相加。
int countNodesAtLevel(TreeNode* root, int k, int level) {
if (root == NULL) {
return 0;
}
if (level == k) {
return 1;
}
int leftCount = countNodesAtLevel(root->left, k, level + 1);
int rightCount = countNodesAtLevel(root->right, k, level + 1);
return leftCount + rightCount;
}
为了方便调用,我们可以再定义一个辅助函数countNodesAtLevelWrapper
,它只接受root
和k
两个参数,内部调用countNodesAtLevel
函数,并将level
初始值设为1。
int countNodesAtLevelWrapper(TreeNode* root, int k) {
return countNodesAtLevel(root, k, 1);
}
查找值为k的节点
查找二叉树中值为k的节点可以使用递归的方式来实现。我们可以定义一个函数findNode
,它接受两个参数:指向二叉树根节点的指针root
和要查找的目标值k
。
函数首先判断根节点是否为NULL
,如果是,则返回NULL
,表示没有找到目标节点。然后,判断根节点的值是否等于目标值k
,如果是,则返回根节点。否则,函数递归地在左子树和右子树中查找目标值为k
的节点,如果在左子树中找到了,就返回左子树中的目标节点;如果在右子树中找到了,就返回右子树中的目标节点;如果都没有找到,则返回NULL
。
TreeNode* findNode(TreeNode* root, int k) {
if (root == NULL) {
return NULL;
}
if (root->val == k) {
return root;
}
TreeNode* leftResult = findNode(root->left, k);
if (leftResult != NULL) {
return leftResult;
}
TreeNode* rightResult = findNode(root->right, k);
if (rightResult != NULL) {
return rightResult;
}
return NULL;
}
示例程序
下面是一个完整的示例程序,展示了如何使用上述函数计算二叉树的节点个数、叶子节点个数、第k层节点个数以及查找值为k的节点。
#include <stdio.h>
#include <stdlib.h>
typedef struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
} TreeNode;
// 创建节点和插入节点的函数代码与上一篇文章相同,这里省略
int countNodes(TreeNode* root) {
if (root == NULL) {
return 0;
}
int leftCount = countNodes(root->left);
int rightCount = countNodes(root->right);
return leftCount + rightCount + 1;
}
int countLeafNodes(TreeNode* root) {
if (root == NULL) {
return 0;
}
if (root->left == NULL && root->right == NULL) {
return 1;
}
int leftLeafCount = countLeafNodes(root->left);
int rightLeafCount = countLeafNodes(root->right);
return leftLeafCount + rightLeafCount;
}
int countNodesAtLevel(TreeNode* root, int k, int level) {
if (root == NULL) {
return 0;
}
if (level == k) {
return 1;
}
int leftCount = countNodesAtLevel(root->left, k, level + 1);
int rightCount = countNodesAtLevel(root->right, k, level + 1);
return leftCount + rightCount;
}
int countNodesAtLevelWrapper(TreeNode* root, int k) {
return countNodesAtLevel(root, k, 1);
}
TreeNode* findNode(TreeNode* root, int k) {
if (root == NULL) {
return NULL;
}
if (root->val == k) {
return root;
}
TreeNode* leftResult = findNode(root->left, k);
if (leftResult != NULL) {
return leftResult;
}
TreeNode* rightResult = findNode(root->right, k);
if (rightResult != NULL) {
return rightResult;
}
return NULL;
}
int main() {
TreeNode* root = NULL;
insertNode(&root, 5);
insertNode(&root, 3);
insertNode(&root, 7);
insertNode(&root, 1);
insertNode(&root, 9);
insertNode(&root, 4);
insertNode(&root, 6);
int totalNodes = countNodes(root);
printf("二叉树的节点个数: %d\n", totalNodes);
int leafNodes = countLeafNodes(root);
printf("二叉树的叶子节点个数: %d\n", leafNodes);
int k = 3;
int nodesAtLevelK = countNodesAtLevelWrapper(root, k);
printf("二叉树第%d层的节点个数: %d\n", k, nodesAtLevelK);
int target = 7;
TreeNode* targetNode = findNode(root, target);
if (targetNode != NULL) {
printf("找到值为%d的节点\n", target);
} else {
printf("未找到值为%d的节点\n", target);
}
return 0;
}
在上面的示例程序中,我们首先创建了一个二叉树,然后分别调用countNodes
、countLeafNodes
、countNodesAtLevelWrapper
和findNode
函数,计算二叉树的节点个数、叶子节点个数、第k层节点个数以及查找值为k的节点。
运行该程序,输出结果如下:
二叉树的节点个数: 7
二叉树的叶子节点个数: 4
二叉树第3层的节点个数: 2
找到值为7的节点
可以看到,程序正确地计算了二叉树的节点个数、叶子节点个数、第k层节点个数,并成功找到了值为7的节点。
总结
本文在上一篇文章的基础上,进一步介绍了二叉树的一些常见操作,包括计算二叉树的节点个数、叶子节点个数、第k层节点个数以及查找值为k的节点。我们使用递归的方式实现了这些操作,并通过示例程序演示了如何使用这些函数。
对于初学者来说,理解和掌握这些二叉树的基本操作非常重要,它们是学习更复杂的树形结构和算法的基础。希望本文能够帮助你更好地理解二叉树的相关概念和操作。如果你有任何问题或建议,欢迎留言交流。