-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path101.Symmetric_Tree.cpp
More file actions
199 lines (165 loc) · 4.78 KB
/
101.Symmetric_Tree.cpp
File metadata and controls
199 lines (165 loc) · 4.78 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include <vector>
#include <iostream>
#include <cstdlib>
using namespace std;
//Definition for binary tree
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
bool isSymmetric(TreeNode *root) {
//return _isSymmetricByLine(root);
return _isSymmetricTreeByRecursion(root);
}
bool _isSymmetricByLine(TreeNode * root) {
int line_size = 1;
TreeNode **line = (TreeNode **)malloc(sizeof(TreeNode *));
line[0] = root;
while (!_isEmptyLine(line, line_size)) {
TreeNode **next_line = (TreeNode **)malloc(sizeof(TreeNode *) * line_size * 2);
for (int i = 0; i < line_size; i++) {
TreeNode* node = line[i];
if (node == NULL) {
next_line[i * 2] = NULL;
next_line[i * 2 + 1] = NULL;
}
else {
next_line[i * 2] = node->left;
next_line[i * 2 + 1] = node->right;
}
}
free(line);
line = next_line;
line_size *= 2;
if (!_isSymmetricLine(line, line_size)) {
free(line);
return false;
}
}
free(line);
return true;
}
bool _isSymmetricLine(TreeNode ** line, int line_size) {
for (int i = 0; i <= line_size / 2; i++) {
TreeNode* head = line[i];
TreeNode* tail = line[line_size - i - 1];
if (head != NULL && tail == NULL) {
return false;
}
if (head == NULL && tail != NULL) {
return false;
}
if (head == NULL && tail == NULL) {
continue;
}
if (head->val != tail->val) {
return false;
}
}
return true;
}
bool _isEmptyLine(TreeNode ** line, int line_size) {
for (int i = 0; i < line_size; i++) {
if (line[i] != NULL) {
return false;
}
}
return true;
}
bool _isSymmetricTreeByRecursion(TreeNode* root) {
if (root == NULL) {
return true;
}
return _isSymmetricTreeByRecursion_Node(root->left, root->right);
}
bool _isSymmetricTreeByRecursion_Node(TreeNode* left, TreeNode* right) {
//Both is NULL
if (left == NULL && right == NULL) {
return true;
}
//one is NULL while another is NOT NULL
if (left == NULL || right == NULL) {
return false;
}
//Both is NOT NULL
if (left->val != right->val) {
return false;
}
if (!_isSymmetricTreeByRecursion_Node(left->left, right->right)) {
return false;
}
if (!_isSymmetricTreeByRecursion_Node(left->right, right->left)) {
return false;
}
return true;
}
};
class Solution_Test {
public:
void test_case_1() {
Solution solution;
TreeNode n1(1);
TreeNode n2(2);
TreeNode n3(2);
n1.left = &n2;
n1.right = &n3;
TreeNode n4(3);
TreeNode n5(4);
n2.left = &n4;
n2.right = &n5;
TreeNode n6(3);
TreeNode n7(4);
n3.left = &n7;
n3.right = &n6;
cout << solution.isSymmetric(&n1) << endl;
};
void test_case_2() {
Solution solution;
TreeNode n1(1);
TreeNode n2(2);
TreeNode n3(2);
n1.left = &n2;
n1.right = &n3;
TreeNode n4(3);
n2.right = &n4;
TreeNode n6(3);
n3.right = &n6;
cout << solution.isSymmetric(&n1) << endl;
};
void test_case_3() {
Solution solution;
TreeNode n1(1);
TreeNode n2(-42);
TreeNode n3(-42);
TreeNode n4(76);
TreeNode n5(76);
TreeNode n6(13);
TreeNode n7(13);
n1.left = &n2;
n1.right = &n3;
n2.right = &n4;
n3.left = &n5;
n4.right = &n6;
n5.right = &n7;
cout << solution.isSymmetric(&n1) << endl;
};
void test_case_4() {
Solution solution;
TreeNode n1(1);
TreeNode n2(2);
n1.left = &n2;
cout << solution.isSymmetric(&n1) << endl;
};
};
int main(int argc, char* argv[]) {
Solution_Test solution_test;
solution_test.test_case_1();
solution_test.test_case_2();
solution_test.test_case_3();
solution_test.test_case_4();
return 0;
};