Problem 4.3 Given a sorted (increasing) array, write an algorithm to create a binary tree with minimal height.
Seems that we should construct a complete binary tree.
Seems that we should construct a complete binary tree.
from queue import *
class binary_tree_node:
def __init__(self, value = None):
self.value = value
self.left = None
self.right = None
def construct_min_btree(array):
# Keep track of current element in the array
i = 0
# Construct while doing BFS
q = queue()
root = binary_tree_node(array[i])
i += 1
q.enqueue(root)
while not q.is_empty():
n = q.dequeue()
# Construct left child
n.left = binary_tree_node(array[i])
i += 1
if i == len(array):
break
q.enqueue(n.left)
# Construct right child
n.right = binary_tree_node(array[i])
i += 1
if