File tree Expand file tree Collapse file tree 1 file changed +49
-0
lines changed Expand file tree Collapse file tree 1 file changed +49
-0
lines changed Original file line number Diff line number Diff line change
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
+ ```
You can’t perform that action at this time.
0 commit comments