The method should return the -th ancestor, or if it does not exist. Use the same binary decomposition loop. If at any point node becomes (past the root), return . Otherwise, after the loop, return the final node. Time: per query.
In the worst case, , so queries take . The method body is just a few lines: a loop over bits of with jumps through the table.