Graph Theory37 sections · 1633 units
Open in Course

Problem - Kth Ancestor of a Tree Node

(LeetCode 1483)

You are given a tree with nn nodes. Implement a class that supports getKthAncestor(node,k)getKthAncestor(node, k) queries. This is exactly the binary lifting problem, but in an object-oriented wrapper.

You will build the upup table in the constructor, then answer queries. Constraints: n50,000n \leq 50,000, up to 50,00050,000 queries. Binary lifting handles this easily. The constructor runs in O(nlogn)O(n \log n) and each query takes O(logn)O(\log n) time.