def toBST(self, A, start, end):
if start < 0 or end < 0 or start > end:
return None
mid = (start + end) / 2
root = TreeNode(A[mid])
#if start != end:
root.left = self.toBST(A,start,mid-1)
root.right = self.toBST(A,mid+1,end)
return root
def sortedArrayToBST(self, A):
# write your code here
return self.toBST(A,0,len(A)-1)
网友评论