Segment Tree
7 min read.
A powerful tree data structure for range queries (sum, min, max) and point/range updates in O(log n). Essential for competitive programming and advanced interview problems.
Think of it this way: Imagine a company hierarchy. To find total sales for a region, the regional manager already has aggregated data from city managers, who have it from store managers. You don't sum every store - you query pre-computed subtotals at just the right level.
Build a sum tree over 6 values; each node stores the sum of a range.
Check your understanding
Answer from memory before you scroll back up. Pulling an idea out of your head is what makes it stay there.
Answer a range query
A segment tree over 8 elements answers sum(2..5). Sort each node by what the query does with it.
Tap a piece, then tap where it goes — or drag it. Tap the tray to take one back.
Fully inside — use its sum
Drop items herePartial — recurse into children
Drop items hereOutside — ignore
Drop items hereQuiz
20 questionsWhat is the key advantage of Segment Tree over Prefix Sum?
Done with this lesson?
Sign in free to keep your progress, streak and quiz scores.