-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_tree_find_cousins.py
More file actions
415 lines (328 loc) · 12.6 KB
/
binary_tree_find_cousins.py
File metadata and controls
415 lines (328 loc) · 12.6 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
'''Two nodes in a binary tree can be called cousins if they are on the same level of the tree but have different parents. For example, in the following diagram 4 and 6 are cousins.
1
/ \
2 3
/ \ \
4 5 6
Given a binary tree and a particular node, find all cousins of that node'''
import logging
from collections import deque
logging.basicConfig(level=logging.INFO, format="%(levelname)s:%(message)s")
class TreeNode:
def __init__(self, val, left=None, right=None):
self.val = val
self.left = left
self.right = right
# --------------------------------------------------
# SOLUTION 1: BFS Level-Order Traversal ⭐ (Optimal)
# --------------------------------------------------
def find_cousins_bfs(root, target):
"""
Find all cousins of target node using BFS (level-order traversal).
Approach:
- Use BFS to traverse tree level by level
- Track parent of each node during traversal
- Find target node's level and parent
- Return all nodes at same level with different parent
Time Complexity: O(n) - visit each node once
Space Complexity: O(w) - where w is maximum width of tree
"""
if root is None:
return []
# BFS to find target node and its parent
target_level = None
target_parent = None
queue = deque([(root, None, 0)]) # (node, parent, level)
level_nodes = {} # Maps level -> list of (node, parent) tuples
while queue:
node, parent, level = queue.popleft()
# Track all nodes at each level with their parents
if level not in level_nodes:
level_nodes[level] = []
level_nodes[level].append((node, parent))
# Found target node
if node.val == target:
target_level = level
target_parent = parent
logging.info(f"Found target {target} at level {level}, parent: {parent.val if parent else None}")
# Add children to queue
if node.left:
queue.append((node.left, node, level + 1))
if node.right:
queue.append((node.right, node, level + 1))
# If target not found
if target_level is None:
logging.warning(f"Target node {target} not found in tree")
return []
# Get all cousins at same level (different parent)
cousins = []
if target_level in level_nodes:
for node, parent in level_nodes[target_level]:
# Cousin if same level but different parent
if parent != target_parent:
cousins.append(node.val)
else:
logging.info(f"Node {node.val} has same parent as target, not a cousin")
logging.info(f"Cousins of {target}: {cousins}")
return sorted(cousins)
# --------------------------------------------------
# SOLUTION 2: DFS with Parent Tracking
# --------------------------------------------------
def find_cousins_dfs(root, target):
"""
Find cousins using DFS with parent and depth tracking.
Approach:
- DFS traversal tracking parent and depth
- Find target's depth and parent first
- Second pass: collect all nodes at same depth with different parent
Time Complexity: O(n) - two passes through tree
Space Complexity: O(h) - recursion stack
"""
target_depth = [None]
target_parent = [None]
def find_target(node, parent, depth):
"""Find target node's depth and parent"""
if node is None:
return
if node.val == target:
target_depth[0] = depth
target_parent[0] = parent
logging.info(f"Found target {target} at depth {depth}, parent: {parent.val if parent else None}")
return
find_target(node.left, node, depth + 1)
find_target(node.right, node, depth + 1)
def collect_cousins(node, parent, depth):
"""Collect all nodes at target depth with different parent"""
if node is None:
return []
cousins = []
# If at target depth
if depth == target_depth[0]:
# Add if different parent
if parent != target_parent[0]:
cousins.append(node.val)
logging.info(f"Found cousin: {node.val}")
# Continue DFS if not at target depth
if depth < target_depth[0]:
cousins.extend(collect_cousins(node.left, node, depth + 1))
cousins.extend(collect_cousins(node.right, node, depth + 1))
return cousins
# First pass: find target
find_target(root, None, 0)
if target_depth[0] is None:
logging.warning(f"Target node {target} not found in tree")
return []
# Second pass: collect cousins
cousins = collect_cousins(root, None, 0)
return sorted(cousins)
# --------------------------------------------------
# SOLUTION 3: Single Pass DFS (Optimized)
# --------------------------------------------------
def find_cousins_single_pass(root, target):
"""
Find cousins in single DFS pass.
Approach:
- Single DFS traversal
- Track all nodes at each depth with their parents
- Return nodes at target depth with different parent
Time Complexity: O(n)
Space Complexity: O(h)
"""
depth_map = {} # Maps depth -> list of (node_value, parent_value)
target_depth = [None]
target_parent = [None]
def dfs(node, parent, depth):
if node is None:
return
# Track node at this depth
if depth not in depth_map:
depth_map[depth] = []
parent_val = parent.val if parent else None
depth_map[depth].append((node.val, parent_val))
# Update target info if found
if node.val == target:
target_depth[0] = depth
target_parent[0] = parent_val
logging.info(f"Found target {target} at depth {depth}, parent: {parent_val}")
dfs(node.left, node, depth + 1)
dfs(node.right, node, depth + 1)
dfs(root, None, 0)
if target_depth[0] is None:
logging.warning(f"Target node {target} not found in tree")
return []
# Find cousins at same depth with different parent
cousins = []
if target_depth[0] in depth_map:
for node_val, parent_val in depth_map[target_depth[0]]:
if parent_val != target_parent[0]:
cousins.append(node_val)
logging.info(f"Cousins of {target}: {cousins}")
return sorted(cousins)
# --------------------------------------------------
# SOLUTION 4: BFS with Early Termination
# --------------------------------------------------
def find_cousins_bfs_optimized(root, target):
"""
BFS approach that stops after finding target's level cousins.
Time Complexity: O(n) in worst case, O(n/2) on average
Space Complexity: O(w) - maximum width
"""
if root is None:
return []
queue = deque([(root, None)]) # (node, parent)
current_level = [root]
target_level = 0
target_parent = None
level = 0
while current_level:
next_level = []
# Process entire level
for node in current_level:
for child in [node.left, node.right]:
if child is None:
continue
# Found target - record level and parent
if child.val == target:
target_level = level + 1
target_parent = node
logging.info(f"Found target {target} at level {target_level}, parent: {node.val}")
next_level.append(child)
current_level = next_level
level += 1
# If target not found
if target_parent is None:
logging.warning(f"Target node {target} not found in tree")
return []
# Now process the target level to find cousins
queue = deque([(root, None)])
current_level = [root]
level = 0
while current_level and level <= target_level:
if level == target_level:
# Collect cousins from current level
cousins = []
for node in current_level:
# Need to find parent - would require different tracking
# This approach is less efficient, kept for comparison
break
next_level = []
for node in current_level:
if node.left:
next_level.append(node.left)
if node.right:
next_level.append(node.right)
current_level = next_level
level += 1
# Alternative: use the BFS solution which is cleaner
return find_cousins_bfs(root, target)
# --------------------------------------------------
# UTILITY FUNCTIONS
# --------------------------------------------------
def print_tree(root, level=0, prefix="Root: "):
"""Pretty print binary tree"""
if root is None:
return
print(" " * (level * 4) + prefix + str(root.val))
if root.left or root.right:
if root.left:
print_tree(root.left, level + 1, "L--- ")
else:
print(" " * ((level + 1) * 4) + "L--- None")
if root.right:
print_tree(root.right, level + 1, "R--- ")
else:
print(" " * ((level + 1) * 4) + "R--- None")
# --------------------------------------------------
# TEST CASES
# --------------------------------------------------
def create_example_tree():
"""Create the example tree from the problem"""
# 1
# / \
# 2 3
# / \ \
# 4 5 6
node4 = TreeNode(4)
node5 = TreeNode(5)
node6 = TreeNode(6)
node2 = TreeNode(2)
node2.left = node4
node2.right = node5
node3 = TreeNode(3)
node3.right = node6
root = TreeNode(1)
root.left = node2
root.right = node3
return root
def run_tests():
print("=" * 70)
print("TESTING FIND COUSINS IN BINARY TREE")
print("=" * 70)
# Test 1: Example from problem
print("\n--- Test 1: Example from Problem ---")
root1 = create_example_tree()
print("\nTree structure:")
print_tree(root1)
test_values = [4, 5, 6, 2, 3, 1]
expected = {
4: [6], # 4 and 6 are on same level but different parents
5: [6], # 5 and 6 are on same level but different parents
6: [4, 5], # 6 and 4,5 are on same level but different parents
2: [3], # 2 and 3 are on same level but different parents
3: [2], # 3 and 2 are on same level but different parents
1: [], # 1 has no cousins (root)
}
for val in test_values:
print(f"\nFinding cousins of {val}:")
result_bfs = find_cousins_bfs(root1, val)
result_dfs = find_cousins_dfs(root1, val)
result_sp = find_cousins_single_pass(root1, val)
exp = expected[val]
print(f" Expected: {exp}")
print(f" BFS Result: {result_bfs} {'✅' if result_bfs == exp else '❌'}")
print(f" DFS Result: {result_dfs} {'✅' if result_dfs == exp else '❌'}")
print(f" Single Pass: {result_sp} {'✅' if result_sp == exp else '❌'}")
# Test 2: Deep tree
print("\n--- Test 2: Deeper Tree ---")
# 1
# / \
# 2 3
# / \ / \
# 4 5 6 7
# /
# 8
node8 = TreeNode(8)
node4 = TreeNode(4)
node4.left = node8
root2 = TreeNode(1)
root2.left = TreeNode(2)
root2.right = TreeNode(3)
root2.left.left = node4
root2.left.right = TreeNode(5)
root2.right.left = TreeNode(6)
root2.right.right = TreeNode(7)
print("\nTree structure:")
print_tree(root2)
print(f"\nCousins of 8: {find_cousins_bfs(root2, 8)}") # []
print(f"Cousins of 4: {find_cousins_bfs(root2, 4)}") # [6, 7]
print(f"Cousins of 5: {find_cousins_bfs(root2, 5)}") # [6, 7]
# Test 3: Single node
print("\n--- Test 3: Single Node Tree ---")
root3 = TreeNode(1)
print("Tree structure:")
print_tree(root3)
print(f"Cousins of 1: {find_cousins_bfs(root3, 1)}") # []
# Test 4: Linear tree
print("\n--- Test 4: Linear Tree (Right chain) ---")
root4 = TreeNode(1)
root4.right = TreeNode(2)
root4.right.right = TreeNode(3)
print("Tree structure:")
print_tree(root4)
print(f"Cousins of 3: {find_cousins_bfs(root4, 3)}") # []
# Test 5: Node not found
print("\n--- Test 5: Node Not Found ---")
root5 = create_example_tree()
print(f"Cousins of 99: {find_cousins_bfs(root5, 99)}") # []
if __name__ == "__main__":
run_tests()