-
Notifications
You must be signed in to change notification settings - Fork 0
/
whetherIsMirror.cpp
56 lines (44 loc) · 1.14 KB
/
whetherIsMirror.cpp
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
#include <iostream>
#include <vector>
#define NIL -1
struct treeNode {
int key;
treeNode* leftChild;
treeNode* rightChild;
};
void traversePreorder(treeNode* head, std::vector<int>& keys) {
if (head == nullptr) {
keys.push_back(NIL);
return;
}
traversePreorder(head->leftChild, keys);
keys.push_back(head->key);
traversePreorder(head->rightChild, keys);
}
void traverseAfterorder(treeNode* head, std::vector<int>& keys) {
if (head == nullptr) {
keys.push_back(NIL);
return;
}
traverseAfterorder(head->rightChild, keys);
keys.push_back(head->key);
traverseAfterorder(head->leftChild, keys);
}
bool whetherIsMirror(treeNode* head) {
std::vector<int> preorder_key;
std::vector<int> aftorder_key;
traversePreorder(head, preorder_key);
traverseAfterorder(head, aftorder_key);
int size1 = preorder_key.size();
int size2 = aftorder_key.size();
if (size1 != size2)
return false;
for (int i = 0; i < size1; i++) {
if (preorder_key[i] != aftorder_key[i])
return false;
}
return true;
}
int main() {
return 0;
}