-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCousinsInBinaryTree.cpp
47 lines (40 loc) · 1.1 KB
/
CousinsInBinaryTree.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
/**
* 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) {}
* };
*/
class Solution {
public:
TreeNode* Parent1 = new TreeNode();
TreeNode* Parent2 = new TreeNode();
int D1, D2;
void Util(TreeNode* root, TreeNode* Parent, int Depth, int X, int Y){
if(root==NULL){
return;
}
if(root->val == X){
Parent1 = Parent;
D1 = Depth;
}
if(root->val == Y){
Parent2 = Parent;
D2 = Depth;
}
Util(root->left, root, Depth+1, X, Y);
Util(root->right, root, Depth+1, X, Y);
return;
}
bool isCousins(TreeNode* root, int X, int Y) {
Util(root,NULL,0,X,Y);
if(D1==D2 && Parent1!=Parent2){
return true;
}
return false;
}
};