// 计算度为1的节点个数
int countDegreeOneNodes(struct Node* root) {
if (root == NULL) {
return 0;
} else if ((root->left == NULL && root->right != NULL) || (root->left != NULL && root->right == NULL)) {
// 当前节点为度为1的节点
return 1 + countDegreeOneNodes(root->left) + countDegreeOneNodes(root->right);
} else {
// 当前节点不是度为1的节点,继续递归
return countDegreeOneNodes(root->left) + countDegreeOneNodes(root->right);
}
}
计算二叉树度为1的节点个数
最新推荐文章于 2023-12-13 00:44:20 发布
这段代码定义了一个名为`intcountDegreeOneNodes`的函数,用于计算给定二叉树结构`structNode*root`中度为1的节点数目。它通过递归遍历左右子树来确定每个节点是否为度1节点。
摘要由CSDN通过智能技术生成