-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathminimum_distance_between_two_given_nodes.cpp
64 lines (53 loc) · 1.84 KB
/
minimum_distance_between_two_given_nodes.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// problem link: https://practice.geeksforgeeks.org/problems/min-distance-between-two-given-nodes-of-a-binary-tree/1
// LCA(lowest common ancestor) and use this formula
// Dist(n1, n2) = Dist(root, n1) + Dist(root, n2) - 2*Dist(root, lca)
// for node to root distance, it is simply similar to finding level of a particular node
Node* lca(Node* root ,int n1 ,int n2 )
{
if(root == NULL) return NULL;
if(root->data == n1 || root->data == n2) return root; // case 1
Node* lca1 = lca(root->left, n1, n2);
Node* lca2 = lca(root->right, n1, n2);
if(lca1 != NULL && lca2 != NULL) return root; // case 2
if(lca1 != NULL) return lca1;
else return lca2;
}
void nodeToRoot(Node *root, int n, int level, int* ans)
{
if(root->left == NULL && root->right == NULL)
{
if(root->data == n)
{
*ans = level+1;
return;
}
else
{
level= 0;
return;
}
}
if(root->data == n)
{
*ans = level+1;
return;
}
if(root->left) nodeToRoot(root->left, n, level+1, ans);
if(root->right) nodeToRoot(root->right, n, level+1, ans);
}
int findDist(Node* root, int n1, int n2) {
Node* Lca = lca(root, n1, n2);
int ans1 =0;
nodeToRoot(root, n1, 0, &ans1);
int dist1 = ans1-1;
// cout<<dist1;
int ans2= 0;
nodeToRoot(root, n2, 0, &ans2);
int dist2 = ans2-1;
// cout<<dist2;
int ans3 =0;
nodeToRoot(root, Lca->data, 0, &ans3);
int lcaDist = ans3-1;
// cout<<lcaDist;
return (dist1+dist2 - (2*lcaDist));
}