Search⌘ K
AI Features

Solution: Convert Sorted Array to Binary Search Tree

Discover how to build a height-balanced binary search tree from a sorted array by selecting the middle element as root and recursively constructing left and right subtrees. This lesson helps you understand the recursive approach to maintain balance and optimize tree height.

Statement

Given an array of integers, nums, sorted in ascending order, your task is to construct a height-balanced binary search tree (BST) from this array.

In a height-balanced BST, the difference of heights of the left subtree and right subtree of any node is not more than 1.

Note: There can be multiple valid BSTs for a given input.

Constraints:

  • 11 \leq nums.length 103\leq 10^3
...