forked from Masked-coder11/gfg-POTD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
07.02.2024.cpp
46 lines (35 loc) · 1.1 KB
/
07.02.2024.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
class Solution{
public:
/* Should return minimum distance between a and b
in a tree with given root*/
int dist(Node* root, int a, int d){
if(root==NULL){
return -1;
}
if(root->data==a){
return d;
}
int left=dist(root->left, a, d+1);
int right= dist(root->right, a, d+1);
if(left!=-1) return left;
if(right!=-1) return right;
return -1;
}
Node* findLCA(Node*root, int a, int b){
if(root==NULL) return NULL;
if(root->data==a || root->data==b) return root;
Node* left= findLCA(root->left, a, b);
Node* right= findLCA(root->right, a, b);
if(left==NULL) return right;
if(right==NULL) return left;
return root;
}
int findDist(Node* root, int a, int b) {
// Your code here
Node* lca= findLCA(root, a, b);
int A= dist(root, a, 0);
int B= dist(root, b, 0);
int C= dist(root, lca->data, 0);
return A+B-2*C;
}
};