用C语言写设二叉树中每个结点的元素均为一个字符,按先序遍历的顺序建立二叉链表,编写三个递归算法对二叉树的结点(度为0、1、2)个数进行统计。 输入格式: 多组数据。每组数据一行,为二叉树的前序序列(序列中元素为‘0’时,表示该结点为空)。当输入只有一个“0”时,输入结束。 输出格式: 每组数据输出一行,每行三个数分别为二叉树的度为0、1、2的结点个数。每两个数用空格分隔。输入样例: 在这里给出一组输入。例如: abcd00e00f00ig00h00 abd00e00cf00g00 0 输出样例: 在这里给出相应的输出。例如: 5 0 4 4 0 3
时间: 2023-06-15 20:07:03 浏览: 69
分析:
- 对于度为0的结点,只有空结点才是度为0的结点,所以当结点为空时,度0结点加1;
- 对于度为1的结点,只有左右子树中有一个为空,另一个不为空时,该结点的度为1,因此递归遍历左右子树,如果其中一个为空,另一个不为空,就将该结点的度1结点加1;
- 对于度为2的结点,当该结点的左右子树均不为空时,该结点的度为2,递归遍历左右子树,如果两个子树均不为空,就将该结点的度为2的结点加1。
具体实现见代码:
相关问题
设二叉树中每个结点的元素均为一个字符,按先序遍历的顺序建立二叉链表,编写递归算法交换该二叉树的左右孩子。用C语言写
以下是C语言的递归算法实现:
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct TreeNode {
char data;
struct TreeNode *left;
struct TreeNode *right;
} TreeNode;
// 先序遍历建立二叉树
TreeNode* createBinaryTree() {
char c;
scanf("%c", &c);
if (c == '#') {
return NULL;
}
TreeNode *root = (TreeNode*)malloc(sizeof(TreeNode));
root->data = c;
root->left = createBinaryTree();
root->right = createBinaryTree();
return root;
}
// 交换左右孩子
void exchangeChild(TreeNode *root) {
if (root == NULL) {
return;
}
TreeNode *temp = root->left;
root->left = root->right;
root->right = temp;
exchangeChild(root->left);
exchangeChild(root->right);
}
// 先序遍历输出二叉树
void preOrder(TreeNode *root) {
if (root == NULL) {
printf("# ");
return;
}
printf("%c ", root->data);
preOrder(root->left);
preOrder(root->right);
}
int main() {
printf("请按先序遍历输入二叉树,用#表示空结点:\n");
TreeNode *root = createBinaryTree();
printf("交换前的二叉树:\n");
preOrder(root);
printf("\n");
exchangeChild(root);
printf("交换后的二叉树:\n");
preOrder(root);
printf("\n");
return 0;
}
```
输入样例:
```
请按先序遍历输入二叉树,用#表示空结点:
AB#C##DE#G##
```
输出样例:
```
交换前的二叉树:
A B # # C # # D E # G # #
交换后的二叉树:
A C # # B # # G E # D # #
```
C语言:按先序遍历序列建立一个二叉树的二叉链表,统计二叉树中叶子结点个数和二叉树的深度。
实现思路:
1. 先根据先序遍历序列创建二叉树,如果当前的字符不为 #,则新建一个结点,并将其插入到树中;如果为 #,则返回上一层结点。
2. 统计二叉树中叶子结点的个数,只需要判断当前结点是否为空,如果为空则返回 0;如果不为空,则判断当前结点的左右子树是否为空,如果左右子树都为空,则说明当前结点为叶子结点,返回 1;否则递归遍历左右子树。
3. 求二叉树的深度,只需要判断当前结点是否为空,如果为空则返回 0;否则分别递归遍历左右子树,取左右子树深度的最大值,再加上 1,即为当前结点所在子树的深度。
代码实现:
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct TreeNode {
char data;
struct TreeNode* left;
struct TreeNode* right;
} TreeNode;
// 根据先序遍历序列创建二叉树
TreeNode* createTree() {
char ch;
scanf("%c", &ch);
if (ch == '#') {
return NULL;
}
TreeNode* root = (TreeNode*)malloc(sizeof(TreeNode));
root->data = ch;
root->left = createTree();
root->right = createTree();
return root;
}
// 统计二叉树中叶子结点的个数
int countLeaf(TreeNode* root) {
if (root == NULL) {
return 0;
}
if (root->left == NULL && root->right == NULL) {
return 1;
}
return countLeaf(root->left) + countLeaf(root->right);
}
// 求二叉树的深度
int maxDepth(TreeNode* root) {
if (root == NULL) {
return 0;
}
int leftDepth = maxDepth(root->left);
int rightDepth = maxDepth(root->right);
return leftDepth > rightDepth ? leftDepth + 1 : rightDepth + 1;
}
int main() {
printf("请输入先序遍历序列:\n");
TreeNode* root = createTree();
int numLeaf = countLeaf(root);
int depth = maxDepth(root);
printf("二叉树中叶子结点的个数为:%d\n", numLeaf);
printf("二叉树的深度为:%d\n", depth);
return 0;
}
```