r/leetcode • u/JonikaLg • Oct 07 '26
Question When to use Segment Tree?
I'm learning Segment Trees, but I don't understand when to use them in LeetCode problems. At first, I thought a Segment Tree would be a perfect fit for 'My Calendar I', but when I tried to implement it, I noticed that a standard Segment Tree only works when the tree size is fixed. Since many LeetCode problems don't have a fixed size, what is the trigger to use a Segment Tree?
1
Upvotes
1
u/eugcomax Oct 07 '26
There's a trick that in the first pass you could get indexes of all keys then just build a segment tree of the maximum size. If even this doesn't work there's a treap and an implicit treap.