Skip to main content

108 - Convert Sorted Array to Binary Search Tree

Details

KeyValue
Linkhttps://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/
LanguagePython 3
Runtime128 ms, faster than 52.36% of Python3 online submissions for Convert Sorted Array to Binary Search Tree
Memory Usage15.6 MB, less than 83.29% of Python3 online submissions for Convert Sorted Array to Binary Search Tree
DatastructuresTreeNode
AlgorithmsBinary Search / DFS
ComplexityTime: O(N) Memory: O(logN)

Procedure

  1. ...

Code

# 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]) -> Optional[TreeNode]:
def dfs(left,right):
if left > right: return None
mid = (left+right) // 2
return TreeNode(nums[mid], dfs(left, mid - 1), dfs(mid+1, right))
return dfs(0, len(nums)-1)