-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcousins_in_binary_tree.cpp
More file actions
72 lines (69 loc) · 1.84 KB
/
cousins_in_binary_tree.cpp
File metadata and controls
72 lines (69 loc) · 1.84 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
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
* =====================================================================================
*
* Filename: cousins_in_binary_tree.cpp
*
* Description: 993. Cousins in Binary Tree.
* https://leetcode.com/problems/cousins-in-binary-tree
*
* Version: 1.0
* Created: 10/18/2021 22:14:26
* Revision: none
* Compiler: gcc
*
* Author: YOUR NAME (),
* Organization:
*
* =====================================================================================
*/
#include <cstdio>
#include <queue>
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) {}
};
// DFS
class Solution {
public:
bool isCousins(TreeNode* root, int x, int y) {
std::queue<TreeNode*> nodes;
nodes.push(root);
while (!nodes.empty()) {
bool found_x = false;
bool found_y = false;
bool is_cousin = false;
const int size = nodes.size();
for (int i = 0; i < size; i++) {
TreeNode* ptr = nodes.front();
nodes.pop();
if ((found_x ^ found_y) && (i % 2 == 0)) {
is_cousin = true;
}
if (ptr == nullptr) {
continue;
}
if (ptr->val == x) {
found_x = true;
} else if (ptr->val == y) {
found_y = true;
}
if (found_x && found_y && is_cousin) {
return true;
}
nodes.push(ptr->left);
nodes.push(ptr->right);
}
}
return false;
}
};
int main(int argc, char* argv[]) {
TreeNode* root = nullptr;
const bool is_cousin = Solution().isCousins(root, 4, 5);
printf("Is counsin? %s\n", (is_cousin ? "Yes" : "No"));
return 0;
}