Algorithm

Convert Sorted Array to BST

Trees Pattern

Convert Sorted Array to BST

Given an integer array nums sorted in strictly increasing order, build a height-balanced binary search tree containing exactly those values, and return its root. Binary search tree means every node's value is greater than all values in its left subtree and smaller than all values in its right subtree. Height-balanced means that at every node the two subtree heights differ by at most 1. Several valid trees usually exist; any one of them is accepted.

CONSTRAINTS
  • 1 <= nums.length <= 10⁴
  • -10⁴ <= nums[i] <= 10⁴
  • nums is sorted in strictly increasing order, so there are no duplicates
  • Any height-balanced BST over these values is accepted
EXAMPLE 1
Input: nums = [-10,-3,0,5,9]
Output: [0,-3,9,-10,null,5]
0 is the middle value, so it splits the rest into two equal groups of two, which become the left and right subtrees. The tree [0,-10,5,null,-3,null,9] is equally correct — both are height-balanced and both read back as the original sorted array.
EXAMPLE 2
Input: nums = [1,3]
Output: [3,1]
With an even count there is no exact middle. Taking the upper of the two makes 3 the root with 1 on its left; taking the lower gives [1,null,3]. Both have heights differing by 1 at every node, so both qualify.
EXAMPLE 3
Input: nums = [0]
Output: [0]
One value makes a single node, which is trivially both a search tree and balanced.
EXAMPLE 4
Input: nums = [-10,-3,0,5,9,12,20]
Output: [5,-3,12,-10,0,9,20]
Seven values fill a perfect tree: the middle becomes the root, and each half of three splits the same way. Nothing about the input is special here — repeated halving always produces the flattest tree possible.
Is there more than one correct answer?
Usually yes. Whenever a range has an even number of values, either middle may be chosen, and each choice yields a different but equally valid tree.
What exactly does height-balanced require?
At *every* node, not just the root, the two subtree heights differ by at most 1 — the same condition checked in Balanced Binary Tree.
Can I just insert the values one at a time?
You can, and the result is a BST, but inserting sorted values produces a single right-leaning chain of height N. It satisfies the search property and fails balance completely.
Are duplicates possible?
No, the input is strictly increasing. That is convenient, since duplicates force a policy decision about which side equal values belong on.

Two demands at once: the tree must be a binary search tree (every node bigger than all of its left subtree, smaller than all of its right) and it must be height-balanced (at every node the two sides' heights differ by at most 1, so the tree stays shallow — about log₂N tall).

Why balance matters: the lazy "insert values one by one" gives a legal BST that's useless.

text
insert -10, -3, 0, 5, 9 in order:

  -10
     \
      -3
        \
         0          each value is bigger than all before it,
          \         so it keeps hanging off the right ->
           5        a chain of height 5. Search costs N, not log N.
            \
             9
The sorted array is already the tree's in-order

Read any BST left → node → right (that's in-order) and the values come out sorted — everything smaller than a node before it, everything larger after. So the array we're handed is the in-order reading of the tree we must build. The order of values is fixed; our only freedom is which value tops each range — and that single choice decides the height.

Pick the middle, and balance comes free

Choose a root. Everything before it in the array must go left, everything after must go right — the BST rule leaves no choice. So the split sizes depend only on where you pick:

text
nums =  [ -10  -3   0   5   9 ]
                     ^ pick MIDDLE as root
        left = [-10 -3]     right = [5 9]      halves -> sizes differ by <= 1

Pick the first value instead and the left gets nothing, the right gets everything — the chain again. Pick the middle and both sides are as equal as possible. Recurse the same way on each half and no node can come out lopsided; the range halves every level, so the height is about log₂N.

python
def build(lo, hi):                  # inclusive index range
    if lo > hi:
        return None                 # empty range: no subtree here
    mid = (lo + hi) // 2            # the middle value becomes this subtree's root
    node = Node(nums[mid])
    node.left  = build(lo, mid - 1) # strictly smaller values
    node.right = build(mid + 1, hi) # strictly larger values
    return node

return build(0, len(nums) - 1)
Crucial Notethe base case is lo > hi, not lo == hi. A range with one value is still a real subtree that must produce a leaf; only a crossed range (indices passed each other) is empty and returns null. And the halves use mid - 1 / mid + 1, never mid — the middle value is already this node, so neither side may include it again (pass mid and a one-element range recurses forever). When a range has an even count there's no exact middle; (lo + hi)//2 takes the lower one, and taking the upper is equally valid — just a different accepted tree.
Watch it build

nums = [-10, -3, 0, 5, 9], picking the middle of each range:

text
[-10 -3 | 0 | 5 9]   mid = 0  -> root 0
   |                  left [-10 -3], right [5 9]
   [-10 | -3]  mid = -10 -> left empty, right = -3 (leaf)
              [5 | 9]  mid = 5 -> left empty, right = 9 (leaf)

result:        0
              / \
           -10   5
              \    \
              -3    9

Every node's two sides differ in height by at most 1, and reading it in-order gives back -10, -3, 0, 5, 9. O(N) time (one node per value), O(log N) recursion depth.

The idea to carry

Two takeaways. In-order is the traversal that exposes a BST's sorted order — validating a BST, finding its k-th smallest, or flattening it back to a list are all in-order walks. And when you're handed sorted data and asked for a balanced structure, the middle element is almost always the root — the same halving instinct behind binary search and merge sort, used any time a structure's cost depends on its depth.

Interactive Strategy Visualization

Array to Balanced BST

Binary Split Strategy

Sorted Array Input
-10
-3
0
5
9
0
Explanation
Starting with sorted array. Goal: Pick middle to balance BST.

Strategy

Focus on the recursive nature of trees: solve for subtrees and combine results at the root.

"Divide and Conquer: Subproblem → Recurrence → Result"

O(N log N) Insert Values One By One (Degenerates To O(N²) Chain)
O(N) Time · O(log N) Space Recursive Midpoint Split