-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinDistanceBetweenTwoNodes.java
More file actions
39 lines (30 loc) · 1.03 KB
/
MinDistanceBetweenTwoNodes.java
File metadata and controls
39 lines (30 loc) · 1.03 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
package Trees;
import java.util.Objects;
public class MinDistanceBetweenTwoNodes {
public static Node LCA(Node root, int n1, int n2) {
if (root == null)
return root;
if (root.val == n1 || root.val == 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;
return Objects.requireNonNullElse(left, right);
}
public static int findLevel(Node root, int a, int level) {
if (root == null) return -1;
if (root.val == 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;
}
}