-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsorted_array_balanced_bst.py
56 lines (40 loc) · 1.07 KB
/
sorted_array_balanced_bst.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
class TreeNode():
def __init__(self, x):
self.val = x
self.left = None
self.right = None
def __repr__(self):
return '<TreeNode {}>'.format(self.val)
def sorted_array_to_bst(arr):
"""
Given a sorted array turn it into a balanced binary tree
"""
if len(arr) == 0:
return None
mid = len(arr) / 2
node = TreeNode(arr[mid])
node.left = sorted_array_to_bst(arr[:mid])
node.right = sorted_array_to_bst(arr[mid+1:])
return node
def test_uneven_bst():
nums = [1, 2, 3]
bst = sorted_array_to_bst(nums)
assert bst.val == 2
assert bst.left.val == 1
assert bst.right.val == 3
def test_is_bst():
nums = [1, 2, 3, 4, 5, 6]
bst = sorted_array_to_bst(nums)
assert bst.val == 4
n2 = bst.left
n6 = bst.right
assert n2.val == 2
assert n2.left.val == 1
assert n2.right.val == 3
assert n6.val == 6
assert n6.left.val == 5
assert n6.right is None
def test_single_node_bst():
nums = [1]
bst = sorted_array_to_bst(nums)
assert bst.val == 1