Segment Tree
Video: https://www.youtube.com/watch?v=rYBtViWXYeI
Discrete version of a Segment Tree: A balanced binary tree. O(logn) height given n elements.
Each leaf node (segment) represents an element in the array. Each non-leaf node covers the union of its children’s range.
Operations:
- build(start, end, vals) -> O(n)
- update(index, value) -> O(logn)
- rangeQuery(start, end) -> O(logn + k): where k is the number of reported segments
1 | nums = [2, 1, 5, 3, 4] |
1 | class SegmentTreeNode: |
307 Range Sum Query - Mutable
Leetcode: https://leetcode.com/problems/range-sum-query-mutable/
1 | class Node: |