Convert Sorted Array to BST

Easy

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.

Examples

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

Balanced BST.

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!