Skip to content

Convert Sorted Array to Binary Search Tree

EasyTrees
Decode it first. Clues, constraints, pattern and plan before any code.

Given an integer list nums sorted in strictly increasing order, build a height-balanced binary search tree from it and return the root. Height-balanced means that at every node, the heights of the two subtrees differ by at most 1.

Several trees can be correct. Any height-balanced BST whose in-order walk gives back nums is accepted.

nums = [-10, -3, 0, 5, 9]  →  [0, -3, 9, -10, null, 5]   (or [0, -10, 5, null, -3, null, 9], ...)
nums = [1, 3]              →  [3, 1]   (or [1, null, 3])

Constraints

  • 1 <= nums.length <= 10^4
  • -10^4 <= nums[i] <= 10^4
  • nums is sorted in strictly increasing order.