108. Convert Sorted Array to Binary Search Tree | English | Easy

preview_player
Показать описание
Рекомендации по теме
Комментарии
Автор

class Solution:
def sortedArrayToBST(self, nums: List[int]) -> TreeNode:
def helper(left, right):
if left > right:
return None
mid = left + right >> 1
node = TreeNode(nums[mid])
node.left = helper(left, mid - 1)
node.right = helper(mid + 1, right)
return node
return helper(0, len(nums) - 1)

Runtime: 64 ms, faster than 99.03% of Python3 online submissions for Convert Sorted Array to Binary Search Tree.
Memory Usage: 16.2 MB, less than 22.46% of Python3 online submissions for Convert Sorted Array to Binary Search Tree.

aunnfriends
join shbcf.ru