-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathYoungestCommonAncestor.java
60 lines (53 loc) · 1.82 KB
/
YoungestCommonAncestor.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
import java.util.*;
class Program {
// O(h) time | O(1) space
public static AncestralTree getYoungestCommonAncestor(AncestralTree topAncestor, AncestralTree descendantOne,
AncestralTree descendantTwo) {
// Write your code here.
int depthOne = getDescendantDepth(topAncestor, descendantOne);
int depthTwo = getDescendantDepth(topAncestor, descendantTwo);
if (depthOne > depthTwo) { // one lower than two
AncestralTree ancestorOneAtDescendantTwoLevel = backtrackAncestralTree(descendantOne, depthOne - depthTwo);
return getCommonAncestor(ancestorOneAtDescendantTwoLevel, descendantTwo);
} else { // one higher than two or at the same depth
AncestralTree ancestorTwoAtDescendantOneLevel = backtrackAncestralTree(descendantTwo, depthTwo - depthOne);
return getCommonAncestor(descendantOne, ancestorTwoAtDescendantOneLevel);
}
}
private static int getDescendantDepth(AncestralTree topAncestor, AncestralTree descendant) {
int depth = 0;
while (descendant != topAncestor) {
++depth;
descendant = descendant.ancestor;
}
return depth;
}
private static AncestralTree backtrackAncestralTree(AncestralTree tree, int steps) {
while (steps > 0) {
tree = tree.ancestor;
--steps;
}
return tree;
}
private static AncestralTree getCommonAncestor(AncestralTree descendantOne, AncestralTree descendantTwo) {
while (descendantOne != descendantTwo) {
descendantOne = descendantOne.ancestor;
descendantTwo = descendantTwo.ancestor;
}
return descendantOne;
}
static class AncestralTree {
public char name;
public AncestralTree ancestor;
AncestralTree(char name) {
this.name = name;
this.ancestor = null;
}
// This method is for testing only.
void addAsAncestor(AncestralTree[] descendants) {
for (AncestralTree descendant : descendants) {
descendant.ancestor = this;
}
}
}
}