-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode.cpp
More file actions
61 lines (49 loc) · 1.11 KB
/
Code.cpp
File metadata and controls
61 lines (49 loc) · 1.11 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
#include <bits/stdc++.h>
using namespace std;
class BinaryTree
{
public:
int value;
BinaryTree*left;
BinaryTree*right;
BinaryTree(int value){
this->value = value;
left = nullptr;
right = nullptr;
}
};
BinaryTree*invert(BinaryTree*root)
{
if(root==NULL)
{
return NULL;
}
BinaryTree*left = invert(root->left);
BinaryTree*right = invert(root->right);
root->left = right;
root->right = left;
return root;
}
void preorder(BinaryTree*root)
{
if(root==NULL)
{
return;
}
cout<<root->value<<" ";
preorder(root->left);
preorder(root->right);
}
int main() {
BinaryTree*root = new BinaryTree(1);
root->left = new BinaryTree(2);
root->right = new BinaryTree(3);
root->left->left = new BinaryTree(4);
root->left->right = new BinaryTree(5);
root->right->left = new BinaryTree(6);
root->right->right = new BinaryTree(7);
root->left->left->left = new BinaryTree(8);
root->left->left->right = new BinaryTree(9);
BinaryTree*temp = invert(root);
preorder(temp);
}