-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path101.cpp
36 lines (36 loc) · 934 Bytes
/
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
// iteration.cpp
/**
* 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 == NULL)
return 1;
queue<TreeNode*> qe1, qe2;
qe1.push(root->left);
qe2.push(root->right);
while (!qe1.empty() && !qe2.empty()) {
TreeNode* r1 = qe1.front();
TreeNode* r2 = qe2.front();
qe1.pop();
qe2.pop();
if (r1 && r2) {
if (r1->val != r2->val)
return 0;
qe1.push(r1->left);
qe1.push(r1->right);
qe2.push(r2->right);
qe2.push(r2->left);
} else if (r1 != r2)
return 0;
}
return 1;
}
};