반응형
leetcode.com/problems/convert-sorted-array-to-binary-search-tree/
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def sortedArrayToBST(self, nums: List[int]) -> TreeNode:
def recursive(node, lst):
if not lst:
return
i = len(lst)// 2
node.val = lst[i]
node.left = recursive(TreeNode(), lst[:i])
node.right = recursive(TreeNode(), lst[i+1:])
return node
return recursive(TreeNode(), nums)
댓글