MCPcopy Create free account
hub / github.com/algorithmzuo/algorithm-journey / getLca

Method getLca

src/class185/Code01_Wave1.java:174–183  ·  view source on GitHub ↗
(int a, int b)

Source from the content-addressed store, hash-verified

172 }
173
174 public static int getLca(int a, int b) {
175 while (top[a] != top[b]) {
176 if (dep[top[a]] <= dep[top[b]]) {
177 b = fa[top[b]];
178 } else {
179 a = fa[top[a]];
180 }
181 }
182 return dep[a] <= dep[b] ? a : b;
183 }
184
185 public static int getDist(int x, int y) {
186 return dep[x] + dep[y] - (dep[getLca(x, y)] << 1);

Callers 1

getDistMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected