-
Notifications
You must be signed in to change notification settings - Fork 0
/
247.py
60 lines (41 loc) · 1.35 KB
/
247.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
57
58
59
60
"""
Problem:
Given a binary tree, determine whether or not it is height-balanced. A height-balanced
binary tree can be defined as one in which the heights of the two subtrees of any node
never differ by more than one.
"""
from typing import Tuple
from DataStructures.Tree import Node, BinaryTree
def height_helper(node: Node) -> Tuple[int, bool]:
if node.left is None:
left_height, balance_left = 0, True
else:
left_height, balance_left = height_helper(node.left)
if node.right is None:
right_height, balance_right = 0, True
else:
right_height, balance_right = height_helper(node.right)
balance = balance_left and balance_right
current_balance = -1 <= (right_height - left_height) <= 1
height = max(left_height, right_height) + 1
return height, balance and current_balance
def check_balance(tree: BinaryTree) -> bool:
if tree.root is None:
return True
_, balance = height_helper(tree.root)
return balance
if __name__ == "__main__":
tree = BinaryTree()
tree.root = Node(0)
tree.root.left = Node(1)
tree.root.right = Node(2)
tree.root.left.left = Node(3)
tree.root.left.right = Node(4)
print(check_balance(tree))
tree.root.left.right.left = Node(5)
print(check_balance(tree))
"""
SPECS:
TIME COMPLEXITY: O(n)
SPACE COMPLEXITY: O(log(n))
"""