-
Notifications
You must be signed in to change notification settings - Fork 22
/
DistanceBetween2NodesInBinaryTree.java
143 lines (112 loc) · 4.5 KB
/
DistanceBetween2NodesInBinaryTree.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package com.company.amazon;
import com.company.amazon.BinaryTree.Node;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class DistanceBetween2NodesInBinaryTree {
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);
root.right.left.right = new Node(8);
printPath(root, 2);
printPath(root, 3);
printPath(root, 4);
printPath(root, 5);
printPath(root, 6);
printPath(root, 7);
printPath(root, 8);
distanceBetweenTwoNodes(root, 4, 5);
distanceBetweenTwoNodesUsingLevel(root, 4, 5);
distanceBetweenTwoNodesUsingLevel(root, 4, 8);
}
public static void distanceBetweenTwoNodesUsingLevel(Node root, int first, int second) {
Node lcaOfBothNodes = LCA(root, first, second);
int level1 = getLevelOfNode(lcaOfBothNodes, first, 0);
int level2 = getLevelOfNode(lcaOfBothNodes, second, 0);
if (level1 != -1 && level2 != -1)
System.out.println("Distance between " + first + " and " + second + " is " + (level1 + level2));
else
System.out.println("Nodes doesn't exist");
}
public static int getLevelOfNode(Node root, int node, int level) {
if (root == null)
return -1;
if (root.data == node)
return level;
int leftLevel = getLevelOfNode(root.left, node, level + 1);
if (leftLevel == -1) // If Node is not in left subtree
return getLevelOfNode(root.right, node, level + 1);
return leftLevel;
}
/**
* (Distance from Root to Node 1 + Distance from Root to Node 2) - 2 * (Distance from Root to LCA(Node1, Node2))
*
* @param first
* @param second
*/
public static void distanceBetweenTwoNodes(Node root, int first, int second) {
List<Integer> result1 = new ArrayList<>();
List<Integer> result2 = new ArrayList<>();
// Once we have both the paths
if (printPathFromRootToNode(root, first, new int[100], -1, result1) &&
printPathFromRootToNode(root, second, new int[100], -1, result2)) {
int distance1 = result1.size();
int distance2 = result2.size();
Node LCA = LCA(root, first, second);
List<Integer> result3 = new ArrayList<>();
printPathFromRootToNode(root, LCA.data, new int[100], -1, result3);
int lcaDistance = result3.size();
System.out.println("Distance between " + first + " and " + second + " is " + ((distance1 + distance2) - 2 * (lcaDistance)));
} else {
System.out.println("Nodes doesn't exist");
}
}
public static void printPath(Node root, int toNode) {
System.out.print("Path from " + root.data + " to " + toNode + " ===> ");
List<Integer> result = new ArrayList<>();
if (printPathFromRootToNode(root, toNode, new int[100], -1, result)) {
System.out.println(result);
} else {
System.out.println("[ NODE DOESN'T EXIST ]");
}
}
private static boolean printPathFromRootToNode(Node root, int toNode, int[] path, int pathIndex, List<Integer> result) {
if (root == null)
return false;
if (root.data == toNode) {
path[++pathIndex] = root.data;
addToResult(path, pathIndex, result);
return true;
} else {
path[++pathIndex] = root.data;
}
if (printPathFromRootToNode(root.left, toNode, path, pathIndex, result) ||
printPathFromRootToNode(root.right, toNode, path, pathIndex, result)) {
return true;
}
return false;
}
private static void addToResult(int[] path, int pathIndex, List<Integer> result) {
for (int i = 0; i <= pathIndex; i++) {
result.add(path[i]);
}
}
private static Node LCA(Node root, int N1, int N2) {
if (root == null)
return root;
if (root.data == N1 || root.data == N2) {
return root;
}
Node leftLCA = LCA(root.left, N1, N2);
Node rightLCA = LCA(root.right, N1, N2);
if (leftLCA != null && rightLCA != null) {
return root;
}
return leftLCA == null ? rightLCA : leftLCA;
}
}