forked from dishanp/Algorithms-For-Software-Developers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCousins in Binary Tree.cpp
42 lines (41 loc) · 1.14 KB
/
Cousins in Binary Tree.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
/**
* 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* util(TreeNode* root,int x,int dep,int &lev){
if(!root)
return nullptr;
if(root->left&&root->left->val==x||root->right&&root->right->val==x)
{
lev=dep;
return root;
}
TreeNode *l=util(root->left,x,dep+1,lev);
if(l)
return l;
TreeNode *r=util(root->right,x,dep+1,lev);
if(r)
return r;
return 0;
}
bool isCousins(TreeNode* root, int x, int y) {
if(!root)
return false;
int x_dep=-1;
int y_dep=-1;
TreeNode* x_parent=util(root,x,0,x_dep);
TreeNode* y_parent=util(root,y,0,y_dep);
if(x_parent==y_parent||x_dep!=y_dep)
return false;
return true;
}
};