23 · Binary Search Trees

#108 Convert Sorted Array to Binary Search Tree

Easysolve on LeetCode ↗

frequently reported atAirbnbAmazonMicrosoftAppleGoogleMeta

the problem

In plain words

Given a strictly increasing array, build a height-balanced BST (subtree heights differ by at most 1 at every node).

in nums = [-10,-3,0,5,9]

out [0,-3,9,-10,null,5]

[0,-10,5,null,-3,null,9] is also accepted.

in nums = [1,3]

out [3,1]

[1,null,3] is also accepted.

  • 1 ≤ nums.length ≤ 10⁴
  • -10⁴ ≤ nums[i] ≤ 10⁴
  • Strictly increasing

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #105
    2. #110
      Balanced Binary Tree
      Easyin Tree Recursion
    3. #704
      Binary Search
      Easyin Binary Search