Turn a Sorted List Into a Balanced Search Tree
Implement balancedFromSorted
You are given an array of integers sorted in strictly increasing order. Build a height-balanced binary search tree that contains exactly these values and return its root. To make the result unique, always choose the middle element of a range as its root (when the range has an even number of elements, the left one of the two middle elements), and build the left and right subtrees from the elements before and after it.
Copying sub-arrays at every step is easy but wasteful; passing index ranges builds the same tree with no copying.
Example 1:
Input: nums = [3,7,12,18,25,31]
Output: [12,3,25,null,7,18,31]
Example 2:
Input: nums = [-5,0,5]
Output: [0,-5,5]
Example 3:
Input: nums = []
Output: []
+ 13 hidden test cases run on Submit.
Constraints:
- ●
0 ≤ nums.length ≤ 100; nums is sorted in strictly increasing order; −1000 ≤ nums[i] ≤ 1000 - ●
Build a binary search tree that contains exactly these values and is height-balanced (at every node the two subtree heights differ by at most 1) - ●
To make the answer unique: the root of any range of values is its MIDDLE element, and when the range has an even number of elements the LEFT of the two middle elements is used (index (lo + hi) / 2, rounded down) - ●
Return the root of the tree (checked as a level-order list); an empty list gives the empty tree
nums =