forked from vaibhavpathak999/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInvertBinaryTree.cpp
More file actions
66 lines (49 loc) · 1.2 KB
/
Copy pathInvertBinaryTree.cpp
File metadata and controls
66 lines (49 loc) · 1.2 KB
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
#include <iostream>
using namespace std;
struct Node {
int data;
struct Node* left_child;
struct Node* right_child;
};
struct Node* newNode(int data) {
Node* node = new Node;
node->data = data;
node->left_child = NULL;
node->right_child = NULL;
return(node);
}
void invert_tree(struct Node* node) {
if (node == NULL)
return;
else
{
struct Node* temp;
invert_tree(node->left_child);
invert_tree(node->right_child);
temp = node->left_child;
node->left_child = node->right_child;
node->right_child = temp;
}
}
void printTree(struct Node* node)
{
if (node == NULL)
return;
printTree(node->left_child);
cout << node->data << " ";
printTree(node->right_child);
}
int main() {
struct Node *root = newNode(21);
root->left_child = newNode(13);
root->right_child = newNode(45);
root->right_child->left_child = newNode(37);
root->right_child->right_child = newNode(59);
cout << "Inorder traversal: Initial tree ::" << endl;
printTree(root);
invert_tree(root);
cout << endl;
cout << "Inorder traversal: Inverted tree :: \n";
printTree(root);
return 0;
}