nodes = [2, 5, 6, 8, None, 11, 15]
class Node:
def init(self, value):
self.value = value
self.left = None
self.right = None
def height(node):
if node is None:
return 0
return 1 + max(height(node.left), height(node.right))
def is_balanced(node):
if node is None:
return True
lh = height(node.left)
rh = height(node.right)
if abs(lh - rh) <= 1 and is_balanced(node.left) and is_balanced(node.right):
return True
return False
def build_tree(nodes, root, i, n):
if i < n:
temp = Node(nodes[i])
root = temp
root.left = build_tree(nodes, root.left, 2 * i + 1, n)
root.right = build_tree(nodes, root.right, 2 * i + 2, n)
return root
root = None
root = build_tree(nodes, root, 0, len(nodes))
print("height:", height(root))
print("properties: Binary Tree")
print("size:", len(nodes))
print("is_balanced:", is_balanced(root))