-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path101.symmetric-tree.cpp
56 lines (56 loc) · 1.43 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
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
// version 1: recursive version
/*
class Solution {
public:
bool isSymmetric(TreeNode* root) {
if (!root) {
return true;
}
return helper(root->left, root->right);
}
bool helper(TreeNode* left, TreeNode* right) {
if (!left && !right) {
return true;
}
if ((left && !right) || (right && !left) || (left->val != right->val)) {
return false;
}
return helper(left->right, right->left) && helper(left->left, right->right);
}
};
*/
class Solution {
public:
bool isSymmetric(TreeNode* root) {
if (!root) {
return true;
}
queue<TreeNode*> q1, q2;
q1.push(root->left);
q2.push(root->right);
while (!q1.empty()) {
TreeNode *node1 = q1.front(); q1.pop();
TreeNode *node2 = q2.front(); q2.pop();
if (!node1 && !node2) {
continue;
}
if ((node1 && !node2) || (node2 && !node1) || (node2->val != node1->val)) {
return false;
}
q1.push(node1->left);
q1.push(node1->right);
q2.push(node2->right);
q2.push(node2->left);
}
return true;
}
};