-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path101
48 lines (46 loc) · 1.19 KB
/
101
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
/**
* 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) {
return helper(root,root);
}
*/
bool isSymmetric(TreeNode* root) {
if(!root)return true;
//return func(root->left, root->right);
queue<TreeNode*>q;
q.push(root->left);
q.push(root->right);
while(!q.empty()){
auto l=q.front();q.pop();
auto r=q.front();q.pop();
if(!l && !r)
continue;
else if( !l || !r)
return false;
else{
if(l->val != r->val)
return false;
q.push(l->left);
q.push(r->right);
q.push(l->right);
q.push(r->left);
}
}
return true;
}
bool helper(TreeNode* l,TreeNode* r){
if(!l&&!r)return true;
if(l==nullptr||r==nullptr)return false;
return (l->val==r->val)&&helper(l->left,r->right)&&helper(l->right,r->left);
}
};