Given a tree, I need to find the 'k'th node in the path from 'a' to 'b'. That means that I need to find the node at the 'kth' position in the path from node 'a' to node 'b'. I was thinking on the lines of Lowest-common-ancestor and/or heavy-light decomposition but I'm not sure if that's the way to do it. Any guidance in the right direction is appreciated.
Details:
- The tree is NOT a binary tree. It's an undirected graph having n-1 edges, n vertices and no cycles. Just your regular tree
- The tree has vertices numbered from 1 to n. There are n-1 undirected edges connecting n-1 pairs of these vertices
- 'a' and 'b' are any 2 vertices numbered from 1 to n in the tree. We are to find the 'k'th node in the path from 'a' to 'b'. It is guaranteed that the value of 'k' is <= number of nodes in the path from 'a' to 'b'
A BFS applied on every query (kth node from 'a' to 'b') is not an option as the number of queries are large.