-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinvert_tree.cpp
More file actions
84 lines (66 loc) · 1.56 KB
/
invert_tree.cpp
File metadata and controls
84 lines (66 loc) · 1.56 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//
// Created by Mayank Parasar on 2019-12-19.
//
/*
* You are given the root of a binary tree. Invert the binary tree in place.
* That is, all left children should become right children, and all right children should become left children.
Example:
a
/ \
b c
/ \ /
d e f
The inverted version of this tree is as follows:
a
/ \
c b
\ / \
f e d
*/
#include <iostream>
#include <vector>
using namespace std;
struct node {
char val;
node* left_child;
node* right_child;
node(char val_) {
val = val_;
left_child = nullptr;
right_child = nullptr;
}
};
void print_tree(node* node_) { // in-order traversal
if(node_ == nullptr)
return;
// print the in-order tree trraversal
print_tree(node_->left_child);
cout << node_->val << " ";
print_tree(node_->right_child);
}
void invert_tree(node* node_) {
// starting from the root invert
if(node_ == nullptr)
return;
swap(node_->left_child, node_->right_child);
invert_tree(node_->left_child);
invert_tree(node_->right_child);
}
int main() {
node* node_a = new node('a');
node* node_b = new node('b');
node* node_c = new node('c');
node* node_d = new node('d');
node* node_e = new node('e');
node* node_f = new node('f');
node_a->left_child = node_b;
node_a->right_child = node_c;
node_b->left_child = node_d;
node_b->right_child = node_e;
node_c->left_child = node_f;
print_tree(node_a);
invert_tree(node_a);
cout << endl;
print_tree(node_a);
return 0;
}