鱼C论坛

 找回密码
 立即注册
查看: 874|回复: 2

[已解决]数据结构,树的操作

[复制链接]
发表于 2023-11-13 18:38:40 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
请问大家,求一颗树中(不一定是二叉树)度为二的结点个数,怎么用C语言实现?
最佳答案
2023-11-13 18:39:15
使用C语言计算树中度为二的结点个数

要计算一棵树中度为二的节点个数,可以使用深度优先搜索(DFS)的方式遍历整棵树。对于每个节点,我们需要统计其子节点或者子树中度为二的节点个数。

首先,我们需要定义树的数据结构,可以使用指针来表示树节点:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. struct TreeNode {
  4.     int val;
  5.     struct TreeNode** children; // 指向子节点的指针数组
  6.     int numChildren; // 子节点的数量
  7. };
复制代码

接下来,我们可以编写一个递归函数来计算度为二的节点个数。该函数遍历当前节点的子节点,并递归地调用自身以计算子节点或子树中度为二的节点个数。对于当前节点,如果其子节点数量等于2,则将结果计数器加一。
  1. int countDegreeTwoNodes(struct TreeNode* root) {
  2.     if (root == NULL) {
  3.         return 0;
  4.     }
  5.     int count = 0;
  6.     // 遍历当前节点的子节点
  7.     for (int i = 0; i < root->numChildren; i++) {
  8.         count += countDegreeTwoNodes(root->children[i]);
  9.     }
  10.     // 判断当前节点的子节点数量是否为2
  11.     if (root->numChildren == 2) {
  12.         count++;
  13.     }
  14.     return count;
  15. }
复制代码

最后,在主函数中,我们可以构建树并调用上述函数来计算度为二的节点个数。
  1. int main() {
  2.     // 构建树结构并初始化
  3.     // 调用计算函数
  4.     int result = countDegreeTwoNodes(root);
  5.     printf("度为二的节点个数为: %d\n", result);
  6.     return 0;
  7. }
复制代码

通过以上代码,我们可以实现统计树中度为二的节点个数的功能。希望对你有所帮助。如果还有其他问题,请随时提问。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-11-13 18:38:48 | 显示全部楼层
你可以通过遍历树的方式来实现。以下是一个使用C语言的示例代码:

  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. struct TreeNode {
  4.     int data;
  5.     struct TreeNode *left;
  6.     struct TreeNode *right;
  7. };

  8. int countNodesWithDegreeTwo(struct TreeNode *root) {
  9.     if (root == NULL) {
  10.         return 0;
  11.     }
  12.     int count = 0;
  13.     if (root->left != NULL && root->right != NULL) {
  14.         count = 1;
  15.     }
  16.     return count + countNodesWithDegreeTwo(root->left) + countNodesWithDegreeTwo(root->right);
  17. }

  18. int main() {
  19.     // 创建树并初始化
  20.     struct TreeNode *root = (struct TreeNode *)malloc(sizeof(struct TreeNode));
  21.     root->data = 1;
  22.     root->left = (struct TreeNode *)malloc(sizeof(struct TreeNode));
  23.     root->left->data = 2;
  24.     root->left->left = NULL;
  25.     root->left->right = NULL;
  26.     root->right = (struct TreeNode *)malloc(sizeof(struct TreeNode));
  27.     root->right->data = 3;
  28.     root->right->left = (struct TreeNode *)malloc(sizeof(struct TreeNode));
  29.     root->right->left->data = 4;
  30.     root->right->left->left = NULL;
  31.     root->right->left->right = NULL;
  32.     root->right->right = NULL;

  33.     int result = countNodesWithDegreeTwo(root);
  34.     printf("度为二的结点个数为: %d\n", result);

  35.     // 释放内存
  36.     free(root->left);
  37.     free(root->right->left);
  38.     free(root->right);
  39.     free(root);

  40.     return 0;
  41. }
复制代码


这段代码定义了一个树的结构,然后使用递归的方式遍历树,统计具有度为二的节点的个数。在这个例子中,我们假设树的节点包含整数数据。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-11-13 18:39:15 | 显示全部楼层    本楼为最佳答案   
使用C语言计算树中度为二的结点个数

要计算一棵树中度为二的节点个数,可以使用深度优先搜索(DFS)的方式遍历整棵树。对于每个节点,我们需要统计其子节点或者子树中度为二的节点个数。

首先,我们需要定义树的数据结构,可以使用指针来表示树节点:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. struct TreeNode {
  4.     int val;
  5.     struct TreeNode** children; // 指向子节点的指针数组
  6.     int numChildren; // 子节点的数量
  7. };
复制代码

接下来,我们可以编写一个递归函数来计算度为二的节点个数。该函数遍历当前节点的子节点,并递归地调用自身以计算子节点或子树中度为二的节点个数。对于当前节点,如果其子节点数量等于2,则将结果计数器加一。
  1. int countDegreeTwoNodes(struct TreeNode* root) {
  2.     if (root == NULL) {
  3.         return 0;
  4.     }
  5.     int count = 0;
  6.     // 遍历当前节点的子节点
  7.     for (int i = 0; i < root->numChildren; i++) {
  8.         count += countDegreeTwoNodes(root->children[i]);
  9.     }
  10.     // 判断当前节点的子节点数量是否为2
  11.     if (root->numChildren == 2) {
  12.         count++;
  13.     }
  14.     return count;
  15. }
复制代码

最后,在主函数中,我们可以构建树并调用上述函数来计算度为二的节点个数。
  1. int main() {
  2.     // 构建树结构并初始化
  3.     // 调用计算函数
  4.     int result = countDegreeTwoNodes(root);
  5.     printf("度为二的节点个数为: %d\n", result);
  6.     return 0;
  7. }
复制代码

通过以上代码,我们可以实现统计树中度为二的节点个数的功能。希望对你有所帮助。如果还有其他问题,请随时提问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-3 11:29

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表