Skip to content

Commit b6ea686

Browse files
authored
Added YoungestCommonAncestor.java
1 parent 8fd1ea1 commit b6ea686

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed

Medium/YoungestCommonAncestor.java

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package AlgoExpert_Medium;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class YoungestCommonAncestor {
7+
8+
public static AncestralTree getYoungestCommonAncestor(
9+
AncestralTree topAncestor, AncestralTree descendantOne, AncestralTree descendantTwo) {
10+
11+
AncestralTree one = descendantOne;
12+
AncestralTree two = descendantTwo;
13+
14+
List<AncestralTree> list1 = new ArrayList<>();
15+
16+
while(one.ancestor != null ){
17+
if(one.ancestor.name == two.name) {
18+
return two ;
19+
}
20+
list1.add(one.ancestor) ;
21+
one = one.ancestor;
22+
}
23+
24+
one = descendantOne;
25+
26+
List<AncestralTree> list2 = new ArrayList<>();
27+
28+
while(two.ancestor != null ){
29+
if(two.ancestor.name == one.name) {
30+
return one ;
31+
}
32+
list2.add(two.ancestor) ;
33+
two = two.ancestor;
34+
}
35+
36+
// Find the first common element in list 1 and list2.
37+
38+
for(int i=0 ; i<list1.size(); i++) {
39+
40+
for(int j=0; j<list2.size() ; j++) {
41+
if( list1.get(i) == list2.get(j) ) {
42+
return list1.get(i) ;
43+
}
44+
}
45+
46+
}
47+
48+
49+
return null;
50+
}
51+
52+
53+
public static AncestralTree getYoungestCommonAncestorO1Space(
54+
AncestralTree topAncestor, AncestralTree descendantOne, AncestralTree descendantTwo) {
55+
56+
int depthOne = getDescendantDepth(descendantOne, topAncestor);
57+
int depthTwo = getDescendantDepth(descendantTwo, topAncestor) ;
58+
59+
if(depthOne > depthTwo ) {
60+
return backtrackAncestralTree(descendantOne, descendantTwo, depthOne - depthTwo) ;
61+
} else {
62+
return backtrackAncestralTree(descendantTwo, descendantOne, depthTwo - depthOne ) ;
63+
}
64+
65+
}
66+
67+
public static int getDescendantDepth(AncestralTree descendant, AncestralTree topAncestor) {
68+
69+
int depth = 0;
70+
71+
while( descendant != topAncestor ) {
72+
depth++;
73+
descendant = descendant.ancestor;
74+
}
75+
76+
return depth ;
77+
78+
}
79+
80+
public static AncestralTree backtrackAncestralTree(AncestralTree lowerDescendant, AncestralTree higherDescendant, int diff) {
81+
82+
while( diff>0 ) {
83+
lowerDescendant = lowerDescendant.ancestor ;
84+
diff-- ;
85+
}
86+
87+
while(lowerDescendant!=higherDescendant) {
88+
lowerDescendant = lowerDescendant.ancestor;
89+
higherDescendant = higherDescendant.ancestor ;
90+
}
91+
92+
return lowerDescendant;
93+
94+
}
95+
96+
97+
static class AncestralTree {
98+
public char name;
99+
public AncestralTree ancestor;
100+
101+
AncestralTree(char name) {
102+
this.name = name;
103+
this.ancestor = null;
104+
}
105+
106+
// This method is for testing only.
107+
void addAsAncestor(AncestralTree[] descendants) {
108+
for (AncestralTree descendant : descendants) {
109+
descendant.ancestor = this;
110+
}
111+
}
112+
113+
}
114+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy