File tree 1 file changed +56
-0
lines changed
1 file changed +56
-0
lines changed Original file line number Diff line number Diff line change
1
+ // This program inverts a Binary Tree
2
+
3
+ #include < iostream>
4
+
5
+
6
+ struct Node {
7
+ int data;
8
+ Node* left,*right;
9
+
10
+
11
+ };
12
+
13
+ Node* Insert (int data){
14
+ Node* new_node=new Node ();
15
+ new_node->data =data;
16
+ new_node->left =nullptr ;
17
+ new_node->right =nullptr ;
18
+ return new_node;
19
+ }
20
+
21
+ void Display (Node* root){
22
+ if (root==nullptr ){
23
+ return ;
24
+ }
25
+ Display (root->left );
26
+ std::cout<<root->data <<" " ;
27
+ Display (root->right );
28
+ }
29
+
30
+ void Invert (Node* root){
31
+ if (root==nullptr ){
32
+ return ;
33
+ }
34
+ else {
35
+ Node* temp;
36
+ Invert (root->left );
37
+ Invert (root->right );
38
+
39
+ temp=root->left ;
40
+ root->left =root->right ;
41
+ root->right =temp;
42
+ }
43
+ }
44
+
45
+ int main (){
46
+ Node* root=Insert (3 );
47
+ root->left =Insert (4 );
48
+ root->right =Insert (5 );
49
+ root->left ->left =Insert (9 );
50
+ root->left ->right =Insert (10 );
51
+ Display (root);
52
+ Invert (root);
53
+ std::cout<<" \n Binary Tree after Inversion: \n " ;
54
+ Display (root);
55
+ return 0 ;
56
+ }
You can’t perform that action at this time.
0 commit comments