0%
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
class Solution: def sortedArrayToBST(self, nums: List[int]) -> Optional[TreeNode]: def build_tree(list_,left,right) -> Optional[TreeNode]: if left > right: return None mid = (left+right)>>1 root = TreeNode(list_[mid]) root.left = build_tree(list_,left,mid-1) root.right = build_tree(list_,mid+1,right) return root return build_tree(nums,0,len(nums)-1)
|