A segment tree stores aggregate values (sum/min/max) for each segment [l,r] in a binary tree. Leaf nodes = individual elements. Internal nodes = aggregate of children.
Binary Indexed Tree (BIT/Fenwick Tree) is simpler to code than segment tree when you only need prefix sums with point updates. O(log n) both operations, O(n) space.
Array [1,2,3,4,5,6]. Watch how a range query [2..4] touches only O(log n) nodes, then see a point update propagate up the tree.
🧠 Under the Hood — the tree lives in a flat array
Like a binary heap, the segment tree needs no pointer chasing — the structure is implicit in the indices. A node covering [l..r] has children covering [l..mid] and [mid+1..r]; the key theorem is that any query range decomposes into at most 2 nodes per level, which is where O(log n) per query comes from. Every range-query data structure (BIT, sparse table, sqrt blocks) is a different trade-off on this same decomposition idea.
⚖️ When to Use / When NOT to Use
🚫 Common Misconceptions
🎤 Interview Follow-ups
You've mastered 6 advanced techniques: Two Pointers, Sliding Window, Monotonic Stack, Tries, Bit Manipulation, and Segment Trees. You're now ready for Phase 5: LeetCode Hard mastery.