Reputation: 13
I need to find data structure that uses pre-processing on a binary tree and answers the LCA query in O(n) space and O(1) time.
Which data structure helps in this case? Thank you
Upvotes: 0
Views: 505
Reputation: 372764
There are a couple of data structures that solve this problem. The most common approach is to use a data structure for the range minimum query problem, which can be solved with O(n) preprocessing time and O(1) query time, and then using that to solve LCA. I put together a two-part series of lecture slides on this topic for a class I taught earlier this year.
For what it's worth, in practice, there are solutions that use O(n) preprocessing time and O(log n) query time but are faster than the equivalent worst-case efficient structures. The ⟨O(n), O(log n)⟩ hybrid described in the first set of lecture slides is, for most practical purposes, good enough.
Upvotes: 1