思路:递归+二分每个节点作为根节点的时候,左子树形成的数组一定比它小,右子树形成的数组一定比它大,符合有序数组中点的性质
class Solution(object):
def sortedArrayToBST(self, nums):
begin = 0
end = len(nums)-1
if begin>end:
return None
mid = (begin+end)//2
root = TreeNode(nums[mid])
root.left = Solution().sortedArrayToBST(nums[begin:mid])
root.right = Solution().sortedArrayToBST(nums[mid+1:end+1])
return root