-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path0993.cpp
More file actions
31 lines (26 loc) · 933 Bytes
/
Copy path0993.cpp
File metadata and controls
31 lines (26 loc) · 933 Bytes
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
class Solution {
public:
bool isCousins(TreeNode *root, int x, int y) {
if (!root) return {};
bool fx = false, fy = false;
queue<TreeNode *> q;
q.push(root);
for (int lvl = 0; !q.empty(); lvl++) {
for (int t = q.size(); t > 0; t--) {
TreeNode *root = q.front();
q.pop();
if (root->left && root->right)
if ((root->left->val == x && root->right->val == y) ||
(root->left->val == y && root->right->val == x))
return false;
if (root->val == x) fx = true;
if (root->val == y) fy = true;
if (root->left) q.push(root->left);
if (root->right) q.push(root->right);
}
if (fx && fy) return true;
if (fx || fy) return false;
}
return false;
}
};