-
Notifications
You must be signed in to change notification settings - Fork 0
/
BT14.java
74 lines (60 loc) · 1.79 KB
/
BT14.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
public class BT14 {
static class Node {
int data;
Node right;
Node left;
Node(int data) {
this.data = data;
this.left = null;
this.right = null;
}
}
//MINIMUM DISTANCE BETWEEN TWO NODES - FIND LCA THEN ANS=(dist btw LCA and n1) + (dist btw LCA and n2)
static Node LCA(Node root, int n1, int n2) {
if (root == null || root.data == n1 || root.data == n2) {
return root;
}
Node left = LCA(root.left, n1, n2);
Node right = LCA(root.right, n1, n2);
if (left == null) {
return right;
}
if (right == null) {
return left;
}
return root;
}
static int DistLCAandN(Node root, int n) {
if (root == null) {
return -1;
}
if (root.data == n) {
return 0;
}
int leftdist = DistLCAandN(root.left, n);
int rightdist = DistLCAandN(root.right, n);
if (leftdist == -1 && rightdist == -1) {
return -1;
} else if (leftdist == -1) {
return rightdist + 1;
} else {
return leftdist + 1;
}
}
static int MinDist(Node root, int n1, int n2) {
Node lca = LCA(root, n1, n2);
int DistN1 = DistLCAandN(lca, n1);
int DistN2 = DistLCAandN(lca, n2);
return DistN1 + DistN2;
}
public static void main(String[] args) {
Node root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.left.right = new Node(5);
root.right.left = new Node(6);
root.right.right = new Node(7);
System.out.println("MIN DISTANCE BTW 4 AND 5 IS: " + MinDist(root, 4, 2));
}
}