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

Method getLca

src/class185/Code07_HideSeek1.java:262–271  ·  view source on GitHub ↗
(int a, int b)

Source from the content-addressed store, hash-verified

260 }
261
262 public static int getLca(int a, int b) {
263 while (top[a] != top[b]) {
264 if (dep[top[a]] <= dep[top[b]]) {
265 b = fa[top[b]];
266 } else {
267 a = fa[top[a]];
268 }
269 }
270 return dep[a] <= dep[b] ? a : b;
271 }
272
273 public static int getDist(int x, int y) {
274 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