使用c语言编写代码实现二叉树的以下功能:二叉树的创建、二叉树的三种递归遍历、二叉树的复制、统计二叉树的结点数,统计二叉树叶子节点数、计算二叉树的深度、交换二叉树每个结点的左孩和右孩。
时间: 2023-12-01 10:40:34 浏览: 69
以下是使用C语言实现二叉树的各种功能的代码:
1. 二叉树的创建
```c
#include <stdio.h>
#include <stdlib.h>
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
};
struct TreeNode* createTree() {
int val;
scanf("%d", &val);
if (val == -1) {
return NULL;
}
struct TreeNode *node = (struct TreeNode*)malloc(sizeof(struct TreeNode));
node->val = val;
node->left = createTree();
node->right = createTree();
return node;
}
```
2. 二叉树的三种递归遍历
```c
void preorderTraversal(struct TreeNode* root) {
if (root == NULL) {
return;
}
printf("%d ", root->val);
preorderTraversal(root->left);
preorderTraversal(root->right);
}
void inorderTraversal(struct TreeNode* root) {
if (root == NULL) {
return;
}
inorderTraversal(root->left);
printf("%d ", root->val);
inorderTraversal(root->right);
}
void postorderTraversal(struct TreeNode* root) {
if (root == NULL) {
return;
}
postorderTraversal(root->left);
postorderTraversal(root->right);
printf("%d ", root->val);
}
```
3. 二叉树的复制
```c
struct TreeNode* cloneTree(struct TreeNode* root) {
if (root == NULL) {
return NULL;
}
struct TreeNode *node = (struct TreeNode*)malloc(sizeof(struct TreeNode));
node->val = root->val;
node->left = cloneTree(root->left);
node->right = cloneTree(root->right);
return node;
}
```
4. 统计二叉树的结点数
```c
int countNodes(struct TreeNode* root) {
if (root == NULL) {
return 0;
}
return 1 + countNodes(root->left) + countNodes(root->right);
}
```
5. 统计二叉树叶子节点数
```c
int countLeaves(struct TreeNode* root) {
if (root == NULL) {
return 0;
}
if (root->left == NULL && root->right == NULL) {
return 1;
}
return countLeaves(root->left) + countLeaves(root->right);
}
```
6. 计算二叉树的深度
```c
int maxDepth(struct TreeNode* root) {
if (root == NULL) {
return 0;
}
int leftDepth = maxDepth(root->left);
int rightDepth = maxDepth(root->right);
return leftDepth > rightDepth ? leftDepth + 1 : rightDepth + 1;
}
```
7. 交换二叉树每个结点的左孩和右孩
```c
void swapChildren(struct TreeNode* root) {
if (root == NULL) {
return;
}
struct TreeNode *temp = root->left;
root->left = root->right;
root->right = temp;
swapChildren(root->left);
swapChildren(root->right);
}
```
阅读全文
相关推荐
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044901.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)