使用c语言,编写交换二叉树的每个节点的左右孩子
时间: 2024-05-07 14:20:59 浏览: 13
可以使用递归实现交换二叉树的每个节点的左右孩子,具体实现如下:
```c
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
};
void swapTree(struct TreeNode* root) {
if (root == NULL) {
return;
}
// 递归交换左右子树
struct TreeNode* temp = root->left;
root->left = root->right;
root->right = temp;
swapTree(root->left);
swapTree(root->right);
}
```
使用时,只需传入二叉树的根节点即可:
```c
struct TreeNode* root = createTree(); // 创建二叉树
swapTree(root); // 交换二叉树的左右孩子
```
相关问题
用c语言编写已知二叉树采用二叉链表存储结构,设计一个算法,使二叉树中所有结点的左右子树相互交换
好的,您所提出的问题是:如何用c语言编写已知二叉树采用二叉链表存储结构,设计一个算法,使二叉树中所有结点的左右子树相互交换。
以下是该问题的算法实现:
```
struct TreeNode {
int val;
struct TreeNode* left;
struct TreeNode* right;
};
void swap(struct TreeNode* root) {
if (root == NULL) return;
struct TreeNode* tmp = root->left;
root->left = root->right;
root->right = tmp;
swap(root->left);
swap(root->right);
}
```
解释:从根节点开始遍历每个节点,对于每个节点,交换其左右子树。然后递归地对其左右子节点执行相同的操作。最终,整棵二叉树中的每个节点的左右子树都被交换。
使用c语言编写代码实现二叉树的以下功能:二叉树的创建、二叉树的三种递归遍历、二叉树的复制、统计二叉树的结点数,统计二叉树叶子节点数、计算二叉树的深度、交换二叉树每个结点的左孩和右孩。
以下是使用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);
}
```