Convert Sorted Array to BST

EasyArrayBinary SearchSortingLinked ListTreeBinary Search Tree

Description

Given an integer array nums sorted in ascending order, convert it to a height-balanced binary search tree. A height-balanced tree has depth of subtrees differ by at most 1. Return the result as an array.

Examples

Input:nums = [-10,-3,0,5,9]
Output:[0,-10,5,null,-3,null,9]
Explanation:

Choose middle element 0 as root. Left subtree from [-10,-3], right subtree from [5,9]. Recursively balance.

Input:nums = [1]
Output:[1]
Explanation:

Single element array forms a BST with just the root node containing that element.

Input:nums = [2,4,6,8,10,12,14]
Output:[8,4,12,2,6,10,14]
Explanation:

For odd-length array, middle element (8) becomes root. Left subtree contains [2,4,6] with root 4, right subtree contains [10,12,14] with root 12, creating a perfectly balanced BST.

Constraints

  • 1 ≤ nums.length ≤ 10⁴

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!