用C++怎么做左右子树的对调
时间: 2023-09-15 20:05:41 浏览: 39
### 回答1:
左右子树的对调可以通过交换节点的左右指针来实现。假设当前要对调的节点为 `node`,则可以按照以下步骤进行:
1. 暂存 `node` 的左子树指针和右子树指针,分别为 `left` 和 `right`。
2. 将 `node` 的左子树指针指向 `right`。
3. 将 `node` 的右子树指针指向 `left`。
下面是一个简单的示例代码:
```c++
void swapChildren(TreeNode* node) {
if (node == nullptr) {
return;
}
TreeNode* left = node->left;
TreeNode* right = node->right;
node->left = right;
node->right = left;
}
```
其中 `TreeNode` 是二叉树节点的结构体或类。调用 `swapChildren` 函数即可对调相应节点的左右子树。
### 回答2:
在C语言中,可以通过指针操作来实现左右子树的对调。假设有一个二叉树的结构如下:
```c
struct TreeNode {
int val;
struct TreeNode* left;
struct TreeNode* right;
};
```
假设现在要对调某个节点的左右子树,可以调用一个函数,输入为要对调的节点指针:
```c
void swapLeftAndRight(struct TreeNode* node) {
if (node == NULL) {
return;
}
// 交换左右子树的指针
struct TreeNode* temp = node->left;
node->left = node->right;
node->right = temp;
}
```
这个函数首先会检查输入的节点是否为空,如果为空则直接返回。然后在交换左右子树之前,通过一个临时指针保存左子树的地址,然后将左子树的地址赋给右子树指针,再将之前保存的左子树地址赋给左子树指针,即可完成左右子树的对调。
例如,假设存在一个二叉树如下:
```
1
/ \
2 3
/ \ / \
4 5 6 7
```
现在要对调节点1的左右子树,可以调用函数`swapLeftAndRight(&node1)`,其中node1为指向节点1的指针。调用之后,二叉树将变为:
```
1
/ \
3 2
/ \ / \
6 7 4 5
```
这样,左右子树就完成了对调。
### 回答3:
在C语言中,我们可以通过交换左右子树节点的指针来实现左右子树的对调。下面是一个简单的例子:
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct treeNode {
int data;
struct treeNode *left;
struct treeNode *right;
} TreeNode;
void swap(TreeNode **node) {
TreeNode *temp = (*node)->left;
(*node)->left = (*node)->right;
(*node)->right = temp;
}
void mirrorImage(TreeNode *node) {
if (node == NULL) {
return;
}
// 交换左右子树节点
swap(&node);
// 递归对调左子树
mirrorImage(node->left);
// 递归对调右子树
mirrorImage(node->right);
}
void printInOrder(TreeNode *node) {
if (node == NULL) {
return;
}
// 中序遍历左子树
printInOrder(node->left);
// 输出节点数据
printf("%d ", node->data);
// 中序遍历右子树
printInOrder(node->right);
}
int main() {
// 构建一棵二叉树
TreeNode *root = (TreeNode *)malloc(sizeof(TreeNode));
root->data = 1;
TreeNode *node2 = (TreeNode *)malloc(sizeof(TreeNode));
node2->data = 2;
TreeNode *node3 = (TreeNode *)malloc(sizeof(TreeNode));
node3->data = 3;
TreeNode *node4 = (TreeNode *)malloc(sizeof(TreeNode));
node4->data = 4;
TreeNode *node5 = (TreeNode *)malloc(sizeof(TreeNode));
node5->data = 5;
root->left = node2;
root->right = node3;
node2->left = node4;
node2->right = node5;
// 对调左右子树
mirrorImage(root);
// 打印对调后的中序遍历结果
printf("对调后的中序遍历结果:");
printInOrder(root);
return 0;
}
```
此代码中,我们定义了一个二叉树的结构`TreeNode`,其中包含节点数据`data`以及左右子树节点指针`left`和`right`。`swap`函数用于交换左右子树节点的指针,`mirrorImage`函数利用递归实现对整棵树的左右子树进行对调,`printInOrder`函数用于中序遍历并输出树的节点数据。
在`main`函数中,我们构建了一棵二叉树并对其进行左右子树的对调操作,并输出对调后的中序遍历结果。结果为`4 2 5 1 3`,表示对调成功。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)