-
Notifications
You must be signed in to change notification settings - Fork 0
/
296.py
41 lines (29 loc) · 1.02 KB
/
296.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
"""
Problem:
Given a sorted array, convert it into a height-balanced binary search tree.
"""
from typing import List
from DataStructures.Tree import BinarySearchTree
def create_balanced_bst_helper(arr: List[int], tree: BinarySearchTree) -> None:
# based on the fact that a sorted array middle element has approximately (with at
# most difference of 1) equal number of elements on its either side
if len(arr) == 0:
return
mid = len(arr) // 2
tree.add(arr[mid])
create_balanced_bst_helper(arr[:mid], tree)
create_balanced_bst_helper(arr[mid + 1 :], tree)
def create_balanced_bst(arr: List[int]) -> BinarySearchTree:
tree = BinarySearchTree()
create_balanced_bst_helper(arr, tree)
return tree
if __name__ == "__main__":
print(create_balanced_bst([1, 2, 3, 4, 5]))
print(create_balanced_bst([1, 2, 3, 4, 5, 6, 7]))
"""
SPECS:
TIME COMPLEXITY: O(n x log(n))
SPACE COMPLEXITY: O(n)
[time complexity can be reduced to O(n) using node reference inplace of calling
tree.add()]
"""