What is a Segment Tree?
A segment tree stores summaries for nested ranges of an array. The root describes the whole array, and each child describes half of its parent's range.
You need binary trees, recursion or iterative tree indexes, and an associative combine operation. The short version is: precompute range summaries in a tree so one update or query touches only logarithmically many summaries.
The Core Idea: Reuse Whole Segments
In LC 307: Range Sum Query, the array is [1,3,5,7,9,11].
A query for indexes 1 through 4 does not need to visit each value separately.
Which stored nodes can answer it? We take every tree segment lying completely inside the query and combine their sums. Segments outside the range contribute nothing.
It is like totaling sales by using completed weekly reports instead of reopening every receipt. Only the partial weeks at the edges need to be broken into smaller groups.
How a Range Query Chooses Nodes
The Rule You Keep True
Every node stores the correct combined answer for exactly the segment assigned to that node. After an update, that statement must be restored from the changed leaf up to the root.
The operation changes the stored value and neutral element:
- Range sum combines with addition and uses zero for no overlap.
- Range minimum combines with
minand uses infinity for no overlap. - Point update replaces one leaf and recomputes ancestors.
- Range module needs lazy or interval-aware updates when entire ranges change.
LC 307: Point Update is the one that usually trips us up. Changing the array value is only the first step. Every parent whose segment contains that index must be recomputed or later queries will read stale data.
Picking the Right Shape
From Rule to Code
- Define the segment represented by each node and the neutral value for no overlap.
- Build leaves from the array and combine children to fill parents.
- For a query, return on no overlap or full overlap and recurse on partial overlap.
- For an update, change the leaf and recompute every ancestor.
- Return the root or combined query result after all affected nodes are correct.
Build time is O(n), while each point update and range query touches O(log n) tree levels.
When to use it
- •The array changes between range queries.
- •You need repeated sums, minimums, maximums, or other mergeable range results.
- •Both point updates and range queries must be faster than scanning the full range.
- •The operation can combine answers from neighboring segments.
How to identify it
- •The prompt mixes update operations with range queries.
- •A prefix sum answers queries quickly but becomes expensive to rebuild after updates.
- •The queried operation is associative, so segment answers can be combined in groups.
- •The expected complexity is **O(log n)** per update and query.
Key insights
- →Each tree node stores the answer for one contiguous segment of the array.
- →A parent result is produced by combining its two child segments.
- →A range query takes a whole node when its segment lies completely inside the requested range.
- →A point update changes one leaf and every ancestor on its path to the root.
- →The tree is larger than the input because it stores summaries for many nested ranges.
Common mistakes
- 01Using a segment tree for a static array where a prefix sum is simpler.
- 02Mixing inclusive query indexes with half-open tree segments.
- 03Returning zero for a non-overlapping minimum query instead of a neutral value such as infinity.
- 04Updating a leaf without recomputing its ancestors.
- 05Allocating too little storage for a recursive tree representation.