### 思路
1. 使用先序遍历的方式构造二叉树。
2. 使用递归函数 `CreateBiTree` 来构造二叉树。
3. 使用递归函数 `CountNodes` 来统计度为2、度为1和度为0的节点数。
### 伪代码
1. 定义二叉树节点结构 `BiTNode` 和二叉树指针 `BiTree`。
2. 定义 `CreateBiTree` 函数:
- 读取一个字符。
- 如果字符为 `#`,则当前节点为空。
- 否则,创建一个新节点,并递归构造其左子树和右子树。
3. 定义 `CountNodes` 函数:
- 如果当前节点为空,返回。
- 如果当前节点有两个孩子,度为2的节点数加1。
- 如果当前节点有一个孩子,度为1的节点数加1。
- 如果当前节点没有孩子,度为0的节点数加1。
- 递归统计左子树和右子树的节点数。
4. 在 `main` 函数中:
- 调用 `CreateBiTree` 构造二叉树。
- 调用 `CountNodes` 统计节点数。
- 输出度为2、度为1和度为0的节点数。
### C++代码
#include "stdio.h"
#include "malloc.h"
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
typedef char ElemType;
typedef struct BiTNode{
ElemType data;
struct BiTNode *lchild,*rchild; // 左右孩子指针
} BiTNode, *BiTree;
Status CreateBiTree(BiTree &T) {
char ch;
scanf("%c", &ch);
if (ch == '#') {
T = NULL;
} else {
if (!(T = (BiTNode *)malloc(sizeof(BiTNode)))) return ERROR;
T->data = ch; // 生成根结点
CreateBiTree(T->lchild); // 构造左子树
CreateBiTree(T->rchild); // 构造右子树
}
return OK;
}
void CountNodes(BiTree T, int °ree2, int °ree1, int °ree0) {
if (T == NULL) return;
if (T->lchild != NULL && T->rchild != NULL) {
degree2++;
} else if (T->lchild != NULL || T->rchild != NULL) {
degree1++;
} else {
degree0++;
}
CountNodes(T->lchild, degree2, degree1, degree0);
CountNodes(T->rchild, degree2, degree1, degree0);
}
int main() {
BiTree T;
int degree2 = 0, degree1 = 0, degree0 = 0;
CreateBiTree(T);
CountNodes(T, degree2, degree1, degree0);
printf("%d\n", degree2);
printf("%d\n", degree1);
printf("%d\n", degree0);
return 0;
}