-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathBT_Problem_32.java
More file actions
81 lines (63 loc) · 2.24 KB
/
Copy pathBT_Problem_32.java
File metadata and controls
81 lines (63 loc) · 2.24 KB
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
75
76
77
78
79
80
81
package trees.binaryTree;
// Problem Title => Find distance between 2 nodes in a Binary tree
public class BT_Problem_32 {
public static class Node {
int value;
Node left;
Node right;
public Node(int value) {
this.value = value;
}
}
public static Node LCA(Node root, int n1, int n2) {
if (root == null)
return null;
if (root.value == n1 || root.value == n2)
return root;
Node left = LCA(root.left, n1, n2);
Node right = LCA(root.right, n1, n2);
if (left != null && right != null)
return root;
if (left == null && right == null)
return null;
if (left != null)
return LCA(root.left, n1, n2);
else
return LCA(root.right, n1, n2);
}
// Returns level of key k if it is present in
// tree, otherwise returns -1
public static int findLevel(Node root, int a, int level) {
if (root == null)
return -1;
if (root.value == a)
return level;
int left = findLevel(root.left, a, level + 1);
if (left == -1)
return findLevel(root.right, a, level + 1);
return left;
}
public static int findDistance(Node root, int a, int b) {
Node lca = LCA(root, a, b);
int d1 = findLevel(lca, a, 0);
int d2 = findLevel(lca, b, 0);
return d1 + d2;
}
// Driver program to test above functions
public static void main(String[] args) {
// Let us create binary tree given in the above example
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);
root.right.left.right = new Node(8);
System.out.println("Dist(4, 5) = " + findDistance(root, 4, 5));
System.out.println("Dist(4, 6) = " + findDistance(root, 4, 6));
System.out.println("Dist(3, 4) = " + findDistance(root, 3, 4));
System.out.println("Dist(2, 4) = " + findDistance(root, 2, 4));
System.out.println("Dist(8, 5) = " + findDistance(root, 8, 5));
}
}