-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBrinary_tree.py
More file actions
146 lines (118 loc) · 4.29 KB
/
Brinary_tree.py
File metadata and controls
146 lines (118 loc) · 4.29 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
'''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.'''
class Node:
def __init__(self, val, left=None, right=None):
self.val = val
self.left = left
self.right = right
# --------------------------------------------------
# SOLUTION 1: Optimized O(n) approach
# --------------------------------------------------
def is_balanced(root):
"""
Check if a binary tree is height-balanced.
Returns True if balanced, False otherwise.
Time Complexity: O(n) - visit each node once
Space Complexity: O(h) - recursive call stack, where h is height
"""
def check_height(node):
"""
Helper function that returns:
- (-1, False) if the subtree is unbalanced
- (height, True) if the subtree is balanced
"""
if node is None:
return (0, True)
left_height, left_balanced = check_height(node.left)
if not left_balanced:
return (-1, False)
right_height, right_balanced = check_height(node.right)
if not right_balanced:
return (-1, False)
# Check if current node is balanced
if abs(left_height - right_height) > 1:
return (-1, False)
# Current node is balanced, return height
return (max(left_height, right_height) + 1, True)
_, is_balanced_result = check_height(root)
return is_balanced_result
# --------------------------------------------------
# SOLUTION 2: Simple recursive approach (less efficient)
# --------------------------------------------------
def is_balanced_simple(root):
"""
Simple recursive approach to check if a tree is height-balanced.
Time Complexity: O(n^2) - height() is called for each node
Space Complexity: O(h) - recursive call stack
"""
def height(node):
if node is None:
return 0
return 1 + max(height(node.left), height(node.right))
if root is None:
return True
left_height = height(root.left)
right_height = height(root.right)
# Check if current node is balanced and both subtrees are balanced
return (abs(left_height - right_height) <= 1 and
is_balanced_simple(root.left) and
is_balanced_simple(root.right))
# --------------------------------------------------
# TEST CASES
# --------------------------------------------------
if __name__ == "__main__":
# Test Case 1: Balanced tree
# 3
# / \
# 9 20
# / \
# 15 7
root1 = Node(3)
root1.left = Node(9)
root1.right = Node(20)
root1.right.left = Node(15)
root1.right.right = Node(7)
print("Test 1 - Balanced tree:")
print(f" is_balanced: {is_balanced(root1)}") # True
print(f" is_balanced_simple: {is_balanced_simple(root1)}") # True
# Test Case 2: Unbalanced tree
# 1
# /
# 2
# /
# 3
root2 = Node(1)
root2.left = Node(2)
root2.left.left = Node(3)
print("\nTest 2 - Unbalanced tree:")
print(f" is_balanced: {is_balanced(root2)}") # False
print(f" is_balanced_simple: {is_balanced_simple(root2)}") # False
# Test Case 3: Single node (balanced)
root3 = Node(1)
print("\nTest 3 - Single node:")
print(f" is_balanced: {is_balanced(root3)}") # True
print(f" is_balanced_simple: {is_balanced_simple(root3)}") # True
# Test Case 4: Empty tree (balanced)
root4 = None
print("\nTest 4 - Empty tree:")
print(f" is_balanced: {is_balanced(root4)}") # True
print(f" is_balanced_simple: {is_balanced_simple(root4)}") # True
# Test Case 5: Balanced but complex tree
# 1
# / \
# 2 3
# / \
# 4 5
# /
# 6
root5 = Node(1)
root5.left = Node(2)
root5.right = Node(3)
root5.left.left = Node(4)
root5.left.right = Node(5)
root5.left.left.left = Node(6)
print("\nTest 5 - Balanced complex tree:")
print(f" is_balanced: {is_balanced(root5)}") # False (node 2 is unbalanced)
print(f" is_balanced_simple: {is_balanced_simple(root5)}") # False