#include <iostream>
struct Node {
int data;
struct Node *left;
struct Node *right;
Node(int x) {
data = x;
left = NULL;
right = NULL;
}
};
void InOrderTraverse(struct Node *root)
{
if (root == NULL) return;
InOrderTraverse(root->left);
printf("%d ", root->data);
InOrderTraverse(root->right);
}
void mirror(struct Node *root)
{
if (root == NULL) return;
else {
struct Node *tmp = NULL;
mirror(root->left);
mirror(root->right);
tmp = root->left;
root->left = root->right;
root->right = tmp;
}
}
int main(int argc, char** argv)
{
Node *root = new Node(1);
root->left = new Node(3);
root->left->left = new Node(2);
root->left->right = new Node(1);
root->left->right->left = new Node(1);
root->right = new Node(-1);
root->right->left = new Node(4);
root->right->left->left = new Node(1);
root->right->left->right = new Node(2);
root->right->right = new Node(5);
root->right->right->right = new Node(2);
InOrderTraverse(root);
printf("\n");
mirror(root);
InOrderTraverse(root);
printf("\n");
return 0;
}
输出结果:
2 3 1 1 1 1 4 2 -1 5 2
2 5 -1 2 4 1 1 1 1 3 2