forked from TheAlgorithms/C-Plus-Plus
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmirror-tree.cpp
72 lines (56 loc) · 1.06 KB
/
mirror-tree.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include<bits/stdc++.h>
using namespace std;
struct Node
{
int data;
struct Node* left;
struct Node* right;
};
struct Node* newNode(int data)
{
struct Node* node = new Node;
node->data = data;
node->left = node->right = NULL;
return(node);
}
void mirror(Node* root)
{
if (root == NULL)
return;
queue<Node*> q;
q.push(root);
while (!q.empty())
{
Node* curr = q.front();
q.pop();
swap(curr->left, curr->right);
if (curr->left)
q.push(curr->left);
if (curr->right)
q.push(curr->right);
}
}
void inOrder(struct Node* node)
{
if (node == NULL)
return;
inOrder(node->left);
cout << node->data << " ";
inOrder(node->right);
}
int main()
{
struct Node *root = newNode(6);
root->left = newNode(7);
root->right = newNode(8);
root->left->left = newNode(9);
root->left->right = newNode(10);
cout << "\n Inorder traversal of the"
" constructed tree is \n";
inOrder(root);
mirror(root);
cout << "\n Inorder traversal of the "
"mirror tree is \n";
inOrder(root);
return 0;
}