1234567891011121314151617181920 |
- from typing import List, Optional
- from tree import TreeNode
- class Solution:
- def sortedArrayToBST(self, nums: List[int]) -> Optional[TreeNode]:
- if not nums:
- return
- mid = len(nums) // 2
- root = TreeNode(nums[mid])
- if mid == 0:
- return root
- root.left = self.sortedArrayToBST(nums[:mid])
- root.right = self.sortedArrayToBST(nums[mid + 1:])
- return root
|