Compute depth[v] for all nodes (BFS/DFS).
Build the table.
For query : if depth[a] < depth[b], swap them.
Jump up to the same depth as : jump depth[a] - depth[b] steps.
If , return (one is ancestor of the other).
Binary search: for down to , if up[a][j] == up[b][j], jump both.
Return up[a][0] (the LCA).
This runs in time and uses space.