Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions binary_search_trees/array_to_bst.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,20 @@ def __init__(self, value, left = None, right = None):
self.left = left
self.right = right


def arr_to_bst(arr):
""" Given a sorted array, write a function to create a
Balanced Binary Search Tree using the elements in the array.
Return the root of the Binary Search Tree.

Time Complexity = O(logn)
Space Complexity = O(n)
"""
pass

if not arr:
return None

mid = (len(arr)) // 2
root = TreeNode(arr[mid])
root.left = arr_to_bst(arr[:mid])
root.right = arr_to_bst(arr[mid+1:])
return root