#include <iostream>
#include <vector>
using namespace std;
//双链表节点结构
typedef struct treeNode {
int value;
struct treeNode* left;
struct treeNode* right;
treeNode(int x) : value(x), left(nullptr), right(nullptr) {}
} TreeNode;
void mirrorTree(TreeNode* root)
{
if (root == nullptr)
{
return;
}
TreeNode* tempNode = root->left;
root->left = root->right;
root->right = tempNode;
mirrorTree(root->left);
mirrorTree(root->right);
}
void inOrderTraversal(TreeNode* root) {
if (root == nullptr) {
return;
}
inOrderTraversal(root->left);
std::cout << root->value << " ";
inOrderTraversal(root->right);
}
int main()
{
// 构建二叉搜索树
TreeNode* root = new TreeNode(4);
root->left = new TreeNode(2);
root->right = new TreeNode(5);
root->left->left = new TreeNode(1);
root->left->right = new TreeNode(3);
// 4
// 2 5
// 1 3
cout << "初始二叉树: " << endl;
inOrderTraversal(root);
mirrorTree(root);
cout << endl;
cout << "镜像后的二叉树: " << endl;
inOrderTraversal(root);
system("pause");
return 0;
}
二叉树的镜像【c++】
最新推荐文章于 2024-08-19 18:41:12 发布