forked from lemonbashar/java-algo-expert
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathYoungestCommonAncestor.java
More file actions
84 lines (68 loc) · 2.24 KB
/
YoungestCommonAncestor.java
File metadata and controls
84 lines (68 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
82
83
84
package algoexpert.medium;
/*
PROBLEM:
Find the youngest common ancestor to the two descendants
Logic:
If we don't have ancestor property defined in the class
we can map the path to both the nodes and compare the paths to find the common ancestor
If we have ancestor property
then bring both the nodes to the same height & iterate to find the first common ancestor
SOLUTION:
1. Iterate from same depth | time : O(d) | space : O(1)
*/
public class YoungestCommonAncestor
{
public static int getDistance(AncestralTree root, AncestralTree node)
{
int distance = 0;
while (node != root)
{
node = node.ancestor;
distance += 1;
}
return distance;
}
// time : O(d) -> depth of deeper node
public static AncestralTree getYoungestCommonAncestor(
AncestralTree topAncestor, AncestralTree descendantOne, AncestralTree descendantTwo)
{
int distance1 = getDistance(topAncestor, descendantOne);
int distance2 = getDistance(topAncestor, descendantTwo);
// rather than swap, we can create a new function and pass in order
if (distance2 > distance1)
{
// swap
AncestralTree temp = descendantOne;
descendantOne = descendantTwo;
descendantTwo = temp;
int tempDist = distance1;
distance1 = distance2;
distance2 = tempDist;
}
while (distance1 > distance2)
{
descendantOne = descendantOne.ancestor;
distance1 -= 1;
}
while(descendantOne != null && descendantTwo != null && 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;
}
}
}
}