Easy · Trees

Convert sorted array to BST

Given an integer array nums sorted in strictly increasing order, return the root of a height-balanced binary search tree that contains exactly the values of nums. In a height-balanced tree, every node's two subtrees differ in height by at most 1. Any such tree is accepted, and an empty array returns null.

Examples

Example 1

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

Output: [0,-10,5,null,-3,null,9]

Example 2

[1,2,3,4,5,6,7]

Output: [4,2,6,1,3,5,7]

Rebuild it in the studio

Read every interview problem free. Ten rooms need no account. A token opens a problem in full — Pro never counts.

More Trees problems