forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main3.cpp
64 lines (48 loc) · 1.29 KB
/
main3.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
60
61
62
63
64
/// Source : https://leetcode.com/problems/symmetric-tree/description/
/// Author : liuyubobobo
/// Time : 2018-06-02
#include <iostream>
#include <queue>
using namespace std;
/// Definition for a binary tree node.
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
/// Non-Recursive
/// Using two queues to level traverse the root in different directions
///
/// Time Complexity: O(n)
/// Space Complexity: O(n)
class Solution {
public:
bool isSymmetric(TreeNode* root) {
if(root == NULL)
return true;
queue<TreeNode*> q1, q2;
q1.push(root);
q2.push(root);
while(!q1.empty() && !q2.empty()){
TreeNode* node1 = q1.front();
q1.pop();
TreeNode* node2 = q2.front();
q2.pop();
if(node1 == NULL && node2 == NULL)
continue;
if(node1 == NULL || node2 == NULL)
return false;
if(node1->val != node2->val)
return false;
q1.push(node1->left);
q1.push(node1->right);
q2.push(node2->right);
q2.push(node2->left);
}
return q1.empty() && q2.empty();
}
};
int main() {
return 0;
}