Data Structures · Visualization

Segment Tree: Build

Segment Tree

Building a segment tree fills in every node's value, starting from the leaves (which just copy the original array's values) and working upward, combining each pair of children into their parent. Time: O(n) total (each node built once). Space: O(n) for the tree.

Big-O: Build O(n); range query and point update O(log n).

Segment Tree: Build

Segment Tree

Building a segment tree fills in every node's value, starting from the leaves (which just copy the original array's values) and working upward, combining each pair of children into their parent. This is naturally done with a recursive function that splits the range in half at every step.

How Build Works

  1. If the current range covers a single element, make it a leaf storing that element's value.
  2. Otherwise, split the range into two halves.
  3. Recursively build the left half and the right half.
  4. Combine the two halves' values (e.g. add them for a sum tree) to get the current node's value.

Build Worked Example

Input: Build a sum segment tree over [2, 4, 6]

  1. Range [2,4,6] splits into [2,4] and [6]
  2. [2,4] splits into [2] and [4], combine → 6
  3. [6] is a single-element leaf → 6
  4. Combine [2,4]=6 and [6]=6 → root = 12

Output: Root value: 12 (matches sum of [2,4,6])

Try your own input
Segment Tree: Introduction
A binary tree where each node covers a range of the array and stores a combined value (like a sum) for that range — letting range queries skip most of the array.
39
[0…7]
17
[0…3]
22
[4…7]
8
[0…1]
9
[2…3]
13
[4…5]
9
[6…7]
5
[0]
3
[1]
8
[2]
1
[3]
9
[4]
4
[5]
7
[6]
2
[7]
Base array:53819472
Active Leaf
Segment Tree: Introduction
Press ▶ Run to watch it step by step.
Python · Segment Trees in Code
def build(base):
    n = len(base)
    tree = [2, 4, 6]
    for i in range(n):
        tree[n + i] = base[i]        # leaves
    for i in range(n - 1, 0, -1):
        tree[i] = tree[2*i] + tree[2*i+1]
    return tree

# O(n)
# example: build on [2, 4, 6]
Input: Build a sum segment tree over [2, 4, 6]

Behind the Scenes Execution

Press ▶ Run in the visualizer — the call stack, registers and memory will replay here in sync with the code, line by line.

Build Time & Space Complexity

TimeO(n) total (each node built once)
SpaceO(n) for the tree

When to Use Build

  • The required setup step before any range queries or updates can run
  • Preprocessing static data for fast repeated range queries

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits

Ready for the next step?

See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.