/
sorted_list_to_bst.py
52 lines (43 loc) · 1.26 KB
/
sorted_list_to_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
#!/usr/bin/python
# Date: 2020-10-20
#
# Description:
# Given a sorted(increasing order) array with unique integer elements, write an
# algorithm to create a binary search tree with minimal height.
#
# Approach:
# Use divide and conquer array and populate tree nodes
# - Insert into the tree the middle element of the array
# - Insert(into the left subtree) the left subarray elements
# - Insert(into the right subtree) the right subarray elements
#
# Complexity:
# O(N)
class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
def tree_inorder(root):
if root:
tree_inorder(root.left)
print(root.data, end=' ')
tree_inorder(root.right)
def sorted_list_to_bst_utils(A, start, end):
if start > end:
return None
mid = (start + end) // 2
node = Node(A[mid])
node.left = sorted_list_to_bst_utils(A, start, mid - 1)
node.right = sorted_list_to_bst_utils(A, mid + 1, end)
return node
def sorted_list_to_bst(A):
return sorted_list_to_bst_utils(A, 0, len(A) - 1)
def main():
root = sorted_list_to_bst([1, 2, 3])
tree_inorder(root) # 1 2 3
print()
root = sorted_list_to_bst([1, 2, 3, 4, 5])
tree_inorder(root) # 1 2 3 4 5
print()
main()