-
Notifications
You must be signed in to change notification settings - Fork 2
/
symmetric-tree.cpp
58 lines (50 loc) · 1.77 KB
/
symmetric-tree.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
57
58
/**
* 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) {
unordered_map<int, vector<int> > helper;
vector<int> flattened = flatten_tree(root, 1, helper);
bool tree_pal = is_palindrome(flattened);
if ( tree_pal == false ) return false;
for ( auto it = helper.begin(); it != helper.end(); ++it ) {
if ( is_palindrome(it->second) == false ) return false;
}
return true;
}
bool is_palindrome(vector<int> nums) {
vector<int> reversed_nums = nums;
reverse(reversed_nums.begin(), reversed_nums.end());
return reversed_nums == nums;
}
vector<int> flatten_tree(TreeNode *root, int level, unordered_map<int, vector<int>> &helper) {
vector<int> result;
if ( root == NULL ) {
result.push_back(-1);
return result;
}
if ( helper.find(level) == helper.end() ) {
vector<int> nodes;
nodes.push_back(root->val);
helper[level] = nodes;
} else {
helper[level].push_back(root->val);
}
if ( root->left == NULL && root->right == NULL ) {
result.push_back(root->val);
return result;
}
vector<int> result_left = flatten_tree(root->left,level+1, helper);
vector<int> result_right = flatten_tree(root->right, level+1, helper);
result_left.push_back(root->val);
result_left.insert(result_left.end(), result_right.begin(), result_right.end());
return result_left;
}
};