/108. Convert Sorted Array to Binary Search Tree

108. Convert Sorted Array to Binary Search Tree

Easy
Arrays75.3% acceptance

Given a sorted integer array input_array in strictly increasing order, construct a height-balanced binary search tree (BST) from input_array. Return the root node of the BST. The BST should be height-balanced, meaning the depths of the two subtrees of every node differ by no more than one. For testing purposes, represent the tree as a level-order traversal list, where null represents missing children.

Example 1

Input: [2,4,7,11,15]

Output: [7,4,15,2,null,11]

Explanation: A possible height-balanced BST is: root 7, left child 4, right child 15, left child of 4 is 2, right child of 15 is 11.

Example 2

Input: [5,8]

Output: [8,5]

Explanation: A possible height-balanced BST is: root 8, left child 5.

Constraints

  • 1 <= len(input_array) <= 104
  • -104 <= input_array[i] <= 104
  • input_array is sorted in strictly increasing order
Python (current runtime)

Case 1

Input: [1,2,3,4,5,6]

Expected: [4,2,6,1,3,5]

Case 2

Input: [10,20,30]

Expected: [20,10,30]

Case 3

Input: [0]

Expected: [0]

Case 4

Input: [-5,-2,0,3,8]

Expected: [0,-2,8,-5,null,3]