Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def sortedArrayToBST(self, nums):
"""
:type nums: List[int]
:rtype: TreeNode
"""
if len(nums) == 0:
return None
if len(nums) == 1:
return TreeNode(nums[0])
def insert(nums):
if len(nums) == 1:
return TreeNode(nums[0])
if len(nums) == 0:
return None
left = 0
right = len(nums)
mid = (left+right)//2
node = TreeNode(nums[mid])
node.left = insert(nums[:mid])
node.right = insert(nums[mid+1:])
return node
return insert(nums)