把排序数组转换为高度最小的二叉搜索树
2018-03-17 本文已影响11人
只为此心无垠
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)