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

Method getLca

src/class185/Code04_Fantasy1.java:176–185  ·  view source on GitHub ↗
(int a, int b)

Source from the content-addressed store, hash-verified

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

Callers 1

getDistMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected