-
Notifications
You must be signed in to change notification settings - Fork 0
/
BalancedBinaryTree.py
37 lines (33 loc) · 1.3 KB
/
BalancedBinaryTree.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
class Node:
def __init__(self, value, left=None, right=None):
self.value = value
self.left = left
self.right = right
class BinaryTree:
def __init__(self, tuples_lists):
self.nodes = {}
self.head = None
for node, left, right in tuples_lists:
root_node = self.nodes.get(node, Node(node))
self.nodes[node] = root_node
if left:
left_node = self.nodes.get(left, Node(left))
self.nodes[left] = left_node
root_node.left = left_node
if right:
right_node = self.nodes.get(right, Node(right))
self.nodes[right] = right_node
root_node.right = right_node
self.head = self.nodes.get(tuples_lists[0][0])
def isBalancedBinaryTree(self, node):
if not node:
return 0, True
left, balanced = self.isBalancedBinaryTree(node.left)
right, balanced = self.isBalancedBinaryTree(node.right)
if abs(left-right) > 1 or not balanced:
return max(left, right) + 1, False
return max(left, right) + 1, True
bt = BinaryTree([(1,2,3), (2,4,5), (3,6,0), (4,7,8), (7,9,0)])
height, isBalanced = bt.isBalancedBinaryTree(bt.head)
print("height:", height)
print("isBalanced:", isBalanced)