-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathFindDistance.java
74 lines (60 loc) · 2.17 KB
/
FindDistance.java
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
65
66
67
68
69
70
71
72
73
74
/*https://practice.geeksforgeeks.org/problems/min-distance-between-two-given-nodes-of-a-binary-tree/1/*/
class GfG {
boolean n1Present = false, n2Present = false;
int findDist(Node root, int n1, int n2) {
//find the LCA node
Node lca = findLCA(root,n1,n2);
//if any of the two values is not present then return -1
if (!n1Present || !n2Present) return -1;
//find the distances of all three nodes from root
int n1Distance = findDistanceFromRoot(root,n1);
int n2Distance = findDistanceFromRoot(root,n2);
int lcaDistance = findDistanceFromRoot(root,lca.data);
//since the path from root to the lca is traversed twice
return n1Distance+n2Distance-(2*lcaDistance);
}
int findDistanceFromRoot(Node root, int data)
{
//return -1 if reached dead end
if (root == null)
return -1;
//return 0 if data found
if (root.data == data)
return 0;
//find the same for left and right subtrees if data not found yet
int left = findDistanceFromRoot(root.left,data);
int right = findDistanceFromRoot(root.right,data);
//if both are -1 then return -1
if (left == -1 && right == -1) return -1;
//otherwise add 1 to the non -1 value and return
return left == -1 ? right+1 : left+1;
}
Node findLCA(Node root, int n1, int n2)
{
//base case
if (root == null)
return null;
Node temp=null;
//mark the presence of the given nodes
if (root.data == n1)
{
n1Present = true;
temp = root;
}
if (root.data == n2)
{
n2Present = true;
temp = root;
}
// Look for keys in left and right subtrees
Node left = findLCA(root.left, n1, n2);
Node right = findLCA(root.right, n1, n2);
if (temp != null)
return temp;
//if both are non null, root is the LCA
if (left != null && right != null)
return root;
// Otherwise check if left subtree or right subtree is LCA
return (left != null) ? left : right;
}
}