108. Convert Sorted Array to Binary Search Tree
https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/
Python
class Solution:
def sortedArrayToBST(self, nums: List[int]) -> Optional[TreeNode]:
return self._build(nums, 0, len(nums)-1)
def _build(self, nums, left, right):
if left > right:
return
pivot = (left + right) // 2
return TreeNode(
val=nums[pivot],
left=self._build(nums, left, pivot-1),
right=self._build(nums, pivot+1, right)
)