今天去健身房锻炼了身体
这是关于二叉树如何求度为一的节点的个数,同理还能求度为零和二的,不难。
还又复习了一遍前序中序后续的遍历方法,已经可以由任意两种推出二叉树结构了,不过二叉树的样子和模式我还是有点不太能和代码结合去理解,还需要多加练习
include <stdio.h>
include <stdlib.h>
typedef struct TreeNode {
int value;
struct TreeNode *left;
struct TreeNode *right;
} TreeNode;
TreeNode *createTreeNode(int value) {
TreeNode *node = (TreeNode *)malloc(sizeof(TreeNode));
node->value = value;
node->left = NULL;
node->right = NULL;
return node;
}
int countDegreeOneNodes(TreeNode *root) {
if (root == NULL) {
return 0;
}
int count = 0;
if ((root->left!= NULL && root->right == NULL) || (root->left == NULL && root->right!= NULL)) {
count++;
}
return count + countDegreeOneNodes(root->left) + countDegreeOneNodes(root->right);
}
int main() {
TreeNode *root = createTreeNode(1);
root->left = createTreeNode(2);
root->right = createTreeNode(3);
root->left->right = createTreeNode(4);
int result = countDegreeOneNodes(root);
printf("度为 1 的节点个数:%d\n", result);
return 0;
}
标签:求度,right,TreeNode,int,10.22,二叉树,NULL,root,left From: https://www.cnblogs.com/Yunyuzuiluo/p/18493843