# 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 not nums or len(nums)==0:
return None
return self.myTree(nums,0,len(nums)-1)
def myTree(self,nums,l,r):
if l>r:
return None
mid=l+(r-l)/2
root=TreeNode(nums[mid])
root.left=self.myTree(nums,l,mid-1)
root.right=self.myTree(nums,mid+1,r)
return root
# 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 not nums or len(nums)==0:
return None
return self.myTree(nums,0,len(nums)-1)
def myTree(self,nums,l,r):
if l>r:
return None
mid=l+(r-l)/2
root=TreeNode(nums[mid])
root.left=self.myTree(nums,l,mid-1)
root.right=self.myTree(nums,mid+1,r)
return root