-
Notifications
You must be signed in to change notification settings - Fork 8
/
solution.cpp
49 lines (47 loc) · 1.39 KB
/
solution.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
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
/**
* 104 / 104 test cases passed.
* Runtime: 8 ms
* Memory Usage: 10.6 MB
*/
class Solution {
public:
using PTT = pair<TreeNode*, TreeNode*>;
bool isCousins(TreeNode* root, int x, int y) {
queue<PTT> que;
que.push( {root, nullptr} );
while (!que.empty()) {
size_t sz = que.size();
vector<PTT> level;
while (sz--) {
auto [node, parent] = que.front();
if (node->val == x || node->val == y) {
level.push_back( {node, parent} );
}
if (node->left) {
que.push( {node->left, node} );
}
if (node->right) {
que.push( {node->right, node} );
}
que.pop();
}
if (level.size() == 0) continue;
if (level.size() == 1) return false;
if (level.size() == 2) {
return level[0].second != level[1].second;
}
}
return false;
}
};