-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path101.cpp
65 lines (56 loc) · 1.56 KB
/
101.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
59
60
61
62
63
64
65
// Author: Tong Xu
// Date: 1/1/2020
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isSymmetric(TreeNode* root) {
if(!root) return true;
height = calHeight(root);
return bfs(root);
}
private:
int height;
int calHeight(TreeNode* root) {
if(!root) return 0;
return 1 + max(calHeight(root->left), calHeight(root->right));
}
bool check(vector<string>& v) {
if(v.empty()) return true;
int l = 0, r = v.size()-1;
while(l <= r) {
if(v[l] != v[r]) return false;
l++, r--;
}
return true;
}
bool bfs(TreeNode* root) {
queue<TreeNode*> nodeQueue;
TreeNode* cur;
int size;
nodeQueue.push(root);
int level = 0;
while(!nodeQueue.empty() && level < height) {
size = nodeQueue.size();
vector<string> tmpVec;
for(int i = 0; i < size; i++) {
cur = nodeQueue.front();
if(cur) tmpVec.push_back(to_string(cur->val));
else tmpVec.push_back("#");
nodeQueue.pop();
cur && cur->left ? nodeQueue.push(cur->left) : nodeQueue.push(NULL);
cur && cur->right ? nodeQueue.push(cur->right) : nodeQueue.push(NULL);
}
if(!check(tmpVec)) return false;
level++;
}
return true;
}
};