Skip to content

Commit a7400c0

Browse files
Merge pull request gzc426#14 from EngineerZhang/EngineerZhang-patch-9
Create 神秘的火柴人.md
2 parents b3b2711 + 521ea11 commit a7400c0

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
```
2+
/**
3+
* Definition for a binary tree node.
4+
* struct TreeNode {
5+
* int val;
6+
* TreeNode *left;
7+
* TreeNode *right;
8+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
9+
* };
10+
*/
11+
class Solution {
12+
public:
13+
bool isSymmetric(TreeNode* root) {
14+
if(!root) return true;
15+
16+
queue<TreeNode*> q1;
17+
queue<TreeNode*> q2;
18+
q1.push(root->left);
19+
q2.push(root->right);
20+
21+
22+
while(!q1.empty() && !q2.empty())
23+
{
24+
TreeNode *l = q1.front();
25+
TreeNode *r = q2.front();
26+
27+
q1.pop();
28+
q2.pop();
29+
30+
if( (!l&&r) || (l&&!r) ) return false;
31+
32+
if(l && r)
33+
{
34+
if(l->val != r->val)
35+
return false;
36+
37+
q1.push(l->left);
38+
q1.push(l->right);
39+
40+
q2.push(r->right);
41+
q2.push(r->left);
42+
}
43+
44+
}
45+
46+
return true;
47+
}
48+
};
49+
```

0 commit comments

Comments
 (0)