Given an integer array nums where the elements are sorted in ascending order, convert it to a height-balanced binary search tree.
A height-balanced binary tree is a binary tree in which the depth of the two subtrees of every node never differs by more than one.
# Definition for a binary tree node.
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution:
def recursion(self,nums:List[int],start:int,end:int)->Optional[TreeNode]:
if start>end:
return None
else:
mid=(start+end)//2
node=TreeNode(nums[mid])
node.left=self.recursion(nums,start,mid-1)
node.right=self.recursion(nums,mid+1,end)
return node
def sortedArrayToBST(self, nums: List[int]) -> Optional[TreeNode]:
return self.recursion(nums,0,len(nums)-1)