-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path101.symmetric-tree.cpp
More file actions
59 lines (44 loc) · 1.35 KB
/
101.symmetric-tree.cpp
File metadata and controls
59 lines (44 loc) · 1.35 KB
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
#include "testharness.h"
#include <map>
#include <string>
#include <string.h>
#include <vector>
#include <queue>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
struct PathStatus {
TreeNode* node;
int tag;
};
public:
bool isSymmetric(TreeNode *root) {
if (root == NULL) return true;
queue<PathStatus> ql;
queue<PathStatus> qr;
if (root->left == NULL && root->right == NULL) return true;
if (root->left == NULL || root->right == NULL) return false;
ql.push({root->left, 1});
qr.push({root->right, 1});
while (!ql.empty() && !qr.empty()) {
auto l = ql.front();
auto r = qr.front();
ql.pop();
qr.pop();
if (l.tag != r.tag || l.node->val != r.node->val) return false;
if (l.node->left != NULL) ql.push({l.node->left, 2 * l.tag - 1});
if (l.node->right != NULL) ql.push({l.node->right, 2 * l.tag + 1});
if (r.node->right != NULL) qr.push({r.node->right, 2 * r.tag - 1});
if (r.node->left != NULL) qr.push({r.node->left, 2 * r.tag + 1});
}
return ql.empty() && qr.empty();
}
};
TEST(Solution, test) {
ASSERT_EQ(2, 1+1);
}