Skip to content
Decoder

Convert Sorted Array to Binary Search Tree

Easy

Problem

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.

Example 1

Input
nums = [-10, -3, 0, 5, 9]
Output
[0, -3, 9, -10, null, 5]
Why
or [0, -10, 5, null, -3, null, 9], ...

Example 2

Input
nums = [1, 3]
Output
[3, 1]
Why
or [1, null, 3]

Constraints

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

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.