-
Notifications
You must be signed in to change notification settings - Fork 0
/
101_Symmetric Tree.cpp
59 lines (48 loc) · 1.38 KB
/
101_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
59
/**
* 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 == 0) return true;
queue<TreeNode *> q1, q2;
TreeNode *left, *right;
q1.push(root->left);
q2.push(root->right);
while(!q1.empty() && !q2.empty())
{
left = q1.front();
q1.pop();
right = q2.front();
q2.pop();
if(left==0 && right==0) continue;
else if(left == 0 || right == 0) return false;
if(left->val != right->val) return false;
q1.push(left->left);
q1.push(left->right);
q2.push(right->right);
q2.push(right->left);
}
return true;
}
};
class Solution {
public:
bool isSymmetric(TreeNode* root) {
if(root==0) return true;
return judge(root->left, root->right);
}
bool judge(TreeNode* s, TreeNode* t)
{
if(s==0 && t == 0) return true;
else if(s==0 || t==0) return false;
if(s->val != t->val) return false;
return judge(s->left, t->right) && judge(s->right, t->left);
}
};