-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcousinsBT.cpp
More file actions
62 lines (60 loc) · 1.73 KB
/
cousinsBT.cpp
File metadata and controls
62 lines (60 loc) · 1.73 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
```
// note: these static variables were declared outside the class Solution
static bool found_x;
static bool found_y;
static int parent_x;
static int parent_y;
// START
// we calculate the height of the tree so that we can perform breadth first search (BFS)
int height(TreeNode* root) {
if(root == NULL) {
return 0;
}
int lheight = height(root->left);
int rheight = height(root->right);
if(lheight>rheight)
return lheight+1;
else
return rheight+1;
}
// here we are simultaneously checking each node we encounter and also tracking the current node's parent
void bfs(TreeNode* node, int level, int target1, int target2, int parent) {
if(node == NULL) {
return;
}
if(level==1){
cout<<node->val<<endl;
if(node->val == target1) {
found_x = true;
parent_x = parent;
}
if(node->val == target2) {
found_y = true;
parent_y = parent;
}
}
bfs(node->left, level-1, target1, target2, node->val);
bfs(node->right, level-1, target1, target2, node->val);
}
bool isCousins(TreeNode* root, int x, int y) {
int ht, x_height, y_height;
ht = height(root);
// for each level we are checking whether the two conditions are true
for(int i=0; i<=ht; i++) {
found_x = false;
found_y = false;
bfs(root, i, x, y, -1);
if( (found_x) && (found_y) ) {
if(parent_x!=parent_y)
return true;
else
return false;
}
// if one is found at a certain level and the other is not, we automatically return false
if(found_x)
return false;
if(found_y)
return false;
}
return false;
}