Repository navigation
Expand file tree
/
Copy pathbinary_tree_problems.py
More file actions
716 lines (560 loc) · 20.2 KB
/
Copy pathbinary_tree_problems.py
File metadata and controls
716 lines (560 loc) · 20.2 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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
"""
BINARY TREE PROBLEMS & SOLUTIONS
===============================
Collection of well-known binary tree problems with optimal solutions.
Binary trees are hierarchical data structures where each node has at most two children.
They form the foundation for many advanced algorithms and data structures.
"""
from collections import deque
class TreeNode:
"""Standard binary tree node definition."""
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
# ============================================================================
# PROBLEM 1: BINARY TREE TRAVERSALS
# ============================================================================
def inorder_traversal(root):
"""
Problem: Traverse binary tree in inorder (left, root, right).
Approach: Recursive and iterative solutions
Time Complexity: O(n)
Space Complexity: O(h) where h is height
"""
result = []
def inorder_recursive(node):
if node:
inorder_recursive(node.left)
result.append(node.val)
inorder_recursive(node.right)
inorder_recursive(root)
return result
def inorder_iterative(root):
"""Iterative inorder using stack."""
result = []
stack = []
current = root
while stack or current:
# Go to leftmost node
while current:
stack.append(current)
current = current.left
# Process current node
current = stack.pop()
result.append(current.val)
# Move to right subtree
current = current.right
return result
def preorder_traversal(root):
"""Preorder traversal: root, left, right."""
result = []
def preorder(node):
if node:
result.append(node.val)
preorder(node.left)
preorder(node.right)
preorder(root)
return result
def postorder_traversal(root):
"""Postorder traversal: left, right, root."""
result = []
def postorder(node):
if node:
postorder(node.left)
postorder(node.right)
result.append(node.val)
postorder(root)
return result
# ============================================================================
# PROBLEM 2: LEVEL ORDER TRAVERSAL
# ============================================================================
def level_order(root):
"""
Problem: Traverse tree level by level.
Approach: BFS using queue
Time Complexity: O(n)
Space Complexity: O(w) where w is maximum width
Example: [3,9,20,null,null,15,7] → [[3],[9,20],[15,7]]
"""
if not root:
return []
result = []
queue = deque([root])
while queue:
level_size = len(queue)
current_level = []
for _ in range(level_size):
node = queue.popleft()
current_level.append(node.val)
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
result.append(current_level)
return result
def zigzag_level_order(root):
"""
Problem: Level order traversal in zigzag pattern.
Example: [3,9,20,null,null,15,7] → [[3],[20,9],[15,7]]
"""
if not root:
return []
result = []
queue = deque([root])
left_to_right = True
while queue:
level_size = len(queue)
current_level = []
for _ in range(level_size):
node = queue.popleft()
current_level.append(node.val)
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
if not left_to_right:
current_level.reverse()
result.append(current_level)
left_to_right = not left_to_right
return result
# ============================================================================
# PROBLEM 3: MAXIMUM DEPTH OF BINARY TREE
# ============================================================================
def max_depth(root):
"""
Problem: Find maximum depth of binary tree.
Approach: Recursive DFS
Time Complexity: O(n)
Space Complexity: O(h)
Example: [3,9,20,null,null,15,7] → 3
"""
if not root:
return 0
return 1 + max(max_depth(root.left), max_depth(root.right))
def max_depth_iterative(root):
"""Iterative approach using level order traversal."""
if not root:
return 0
queue = deque([(root, 1)])
max_depth = 0
while queue:
node, depth = queue.popleft()
max_depth = max(max_depth, depth)
if node.left:
queue.append((node.left, depth + 1))
if node.right:
queue.append((node.right, depth + 1))
return max_depth
# ============================================================================
# PROBLEM 4: VALIDATE BINARY SEARCH TREE
# ============================================================================
def is_valid_bst(root):
"""
Problem: Check if binary tree is valid BST.
Approach: Inorder traversal should be sorted
Time Complexity: O(n)
Space Complexity: O(h)
Example: [2,1,3] → True, [5,1,4,null,null,3,6] → False
"""
def validate(node, min_val, max_val):
if not node:
return True
if node.val <= min_val or node.val >= max_val:
return False
return (validate(node.left, min_val, node.val) and
validate(node.right, node.val, max_val))
return validate(root, float('-inf'), float('inf'))
def is_valid_bst_inorder(root):
"""Alternative: Check if inorder traversal is sorted."""
def inorder(node):
if not node:
return []
return inorder(node.left) + [node.val] + inorder(node.right)
values = inorder(root)
return all(values[i] < values[i + 1] for i in range(len(values) - 1))
# ============================================================================
# PROBLEM 5: SAME TREE
# ============================================================================
def is_same_tree(p, q):
"""
Problem: Check if two binary trees are identical.
Approach: Recursive comparison
Time Complexity: O(min(m, n))
Space Complexity: O(min(m, n))
"""
# Both are None
if not p and not q:
return True
# One is None, other is not
if not p or not q:
return False
# Values are different
if p.val != q.val:
return False
# Recursively check subtrees
return (is_same_tree(p.left, q.left) and
is_same_tree(p.right, q.right))
# ============================================================================
# PROBLEM 6: SYMMETRIC TREE
# ============================================================================
def is_symmetric(root):
"""
Problem: Check if binary tree is symmetric (mirror of itself).
Approach: Recursive helper function
Time Complexity: O(n)
Space Complexity: O(h)
Example: [1,2,2,3,4,4,3] → True
"""
def is_mirror(left, right):
if not left and not right:
return True
if not left or not right:
return False
return (left.val == right.val and
is_mirror(left.left, right.right) and
is_mirror(left.right, right.left))
return not root or is_mirror(root.left, root.right)
# ============================================================================
# PROBLEM 7: BINARY TREE PATHS
# ============================================================================
def binary_tree_paths(root):
"""
Problem: Find all root-to-leaf paths.
Approach: DFS with path tracking
Time Complexity: O(n)
Space Complexity: O(n * h)
Example: [1,2,3,null,5] → ["1->2->5","1->3"]
"""
if not root:
return []
paths = []
def dfs(node, current_path):
if not node:
return
current_path.append(str(node.val))
# If leaf node, add path to result
if not node.left and not node.right:
paths.append("->".join(current_path))
else:
# Continue exploring
dfs(node.left, current_path)
dfs(node.right, current_path)
# Backtrack
current_path.pop()
dfs(root, [])
return paths
# ============================================================================
# PROBLEM 8: LOWEST COMMON ANCESTOR
# ============================================================================
def lowest_common_ancestor(root, p, q):
"""
Problem: Find lowest common ancestor of two nodes.
Approach: Recursive search
Time Complexity: O(n)
Space Complexity: O(h)
"""
if not root or root == p or root == q:
return root
left = lowest_common_ancestor(root.left, p, q)
right = lowest_common_ancestor(root.right, p, q)
# If both left and right are not None, root is LCA
if left and right:
return root
# Return the non-None child
return left or right
def lca_bst(root, p, q):
"""
LCA in BST - optimized version.
Time Complexity: O(h), Space Complexity: O(1)
"""
while root:
if p.val < root.val and q.val < root.val:
root = root.left
elif p.val > root.val and q.val > root.val:
root = root.right
else:
return root
return None
# ============================================================================
# PROBLEM 9: DIAMETER OF BINARY TREE
# ============================================================================
def diameter_of_binary_tree(root):
"""
Problem: Find diameter (longest path between any two nodes).
Approach: Calculate height while tracking max diameter
Time Complexity: O(n)
Space Complexity: O(h)
Example: [1,2,3,4,5] → 3 (path 4->2->1->3 or 5->2->1->3)
"""
max_diameter = 0
def height(node):
nonlocal max_diameter
if not node:
return 0
left_height = height(node.left)
right_height = height(node.right)
# Update diameter through current node
max_diameter = max(max_diameter, left_height + right_height)
return 1 + max(left_height, right_height)
height(root)
return max_diameter
# ============================================================================
# PROBLEM 10: BINARY TREE MAXIMUM PATH SUM
# ============================================================================
def max_path_sum(root):
"""
Problem: Find maximum path sum between any two nodes.
Approach: Post-order traversal with path sum calculation
Time Complexity: O(n)
Space Complexity: O(h)
Example: [1,2,3] → 6 (2->1->3)
"""
max_sum = float('-inf')
def max_gain(node):
nonlocal max_sum
if not node:
return 0
# Max gain from left and right subtrees (ignore negative gains)
left_gain = max(max_gain(node.left), 0)
right_gain = max(max_gain(node.right), 0)
# Path sum through current node
current_max = node.val + left_gain + right_gain
max_sum = max(max_sum, current_max)
# Return max gain including current node
return node.val + max(left_gain, right_gain)
max_gain(root)
return max_sum
# ============================================================================
# PROBLEM 11: CONSTRUCT BINARY TREE FROM TRAVERSALS
# ============================================================================
def build_tree_preorder_inorder(preorder, inorder):
"""
Problem: Build binary tree from preorder and inorder traversals.
Approach: Use preorder for root, inorder for left/right subtrees
Time Complexity: O(n)
Space Complexity: O(n)
"""
if not preorder or not inorder:
return None
# First element in preorder is root
root = TreeNode(preorder[0])
# Find root position in inorder
mid = inorder.index(preorder[0])
# Build left and right subtrees
root.left = build_tree_preorder_inorder(preorder[1:mid+1], inorder[:mid])
root.right = build_tree_preorder_inorder(preorder[mid+1:], inorder[mid+1:])
return root
def build_tree_inorder_postorder(inorder, postorder):
"""Build tree from inorder and postorder traversals."""
if not inorder or not postorder:
return None
# Last element in postorder is root
root = TreeNode(postorder[-1])
# Find root position in inorder
mid = inorder.index(postorder[-1])
# Build left and right subtrees
root.left = build_tree_inorder_postorder(inorder[:mid], postorder[:mid])
root.right = build_tree_inorder_postorder(inorder[mid+1:], postorder[mid:-1])
return root
# ============================================================================
# PROBLEM 12: SERIALIZE AND DESERIALIZE BINARY TREE
# ============================================================================
def serialize(root):
"""
Problem: Serialize binary tree to string.
Approach: Preorder traversal with null markers
Time Complexity: O(n)
Space Complexity: O(n)
"""
def preorder(node):
if not node:
vals.append("null")
else:
vals.append(str(node.val))
preorder(node.left)
preorder(node.right)
vals = []
preorder(root)
return ",".join(vals)
def deserialize(data):
"""Deserialize string back to binary tree."""
def build():
val = next(vals)
if val == "null":
return None
node = TreeNode(int(val))
node.left = build()
node.right = build()
return node
vals = iter(data.split(","))
return build()
# ============================================================================
# PROBLEM 13: FLATTEN BINARY TREE TO LINKED LIST
# ============================================================================
def flatten(root):
"""
Problem: Flatten binary tree to linked list in-place.
Approach: Modified preorder traversal
Time Complexity: O(n)
Space Complexity: O(1)
"""
if not root:
return
current = root
while current:
if current.left:
# Find rightmost node in left subtree
rightmost = current.left
while rightmost.right:
rightmost = rightmost.right
# Connect rightmost to current's right
rightmost.right = current.right
# Move left subtree to right
current.right = current.left
current.left = None
current = current.right
# ============================================================================
# PROBLEM 14: BINARY TREE RIGHT SIDE VIEW
# ============================================================================
def right_side_view(root):
"""
Problem: Return values of nodes you can see from right side.
Approach: Level order traversal, take last node of each level
Time Complexity: O(n)
Space Complexity: O(w)
Example: [1,2,3,null,5,null,4] → [1,3,4]
"""
if not root:
return []
result = []
queue = deque([root])
while queue:
level_size = len(queue)
for i in range(level_size):
node = queue.popleft()
# If last node in level, add to result
if i == level_size - 1:
result.append(node.val)
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
return result
# ============================================================================
# PROBLEM 15: KTHSMALLEST ELEMENT IN BST
# ============================================================================
def kth_smallest(root, k):
"""
Problem: Find kth smallest element in BST.
Approach: Inorder traversal (gives sorted order)
Time Complexity: O(h + k)
Space Complexity: O(h)
"""
def inorder(node):
if not node:
return None
# Search in left subtree
left_result = inorder(node.left)
if left_result is not None:
return left_result
# Process current node
nonlocal k
k -= 1
if k == 0:
return node.val
# Search in right subtree
return inorder(node.right)
return inorder(root)
# ============================================================================
# PROBLEM 16: CONVERT BST TO GREATER TREE
# ============================================================================
def convert_bst(root):
"""
Problem: Convert BST where each node's value is sum of all greater values.
Approach: Reverse inorder traversal (right, root, left)
Time Complexity: O(n)
Space Complexity: O(h)
Example: [4,1,6,0,2,5,7,null,null,null,3,null,null,null,8]
→ [30,36,21,36,35,26,15,null,null,null,33,null,null,null,8]
"""
total = 0
def reverse_inorder(node):
nonlocal total
if node:
reverse_inorder(node.right)
total += node.val
node.val = total
reverse_inorder(node.left)
reverse_inorder(root)
return root
# ============================================================================
# TEST FUNCTIONS
# ============================================================================
def create_test_tree():
"""Create a test binary tree: [3,9,20,null,null,15,7]"""
root = TreeNode(3)
root.left = TreeNode(9)
root.right = TreeNode(20)
root.right.left = TreeNode(15)
root.right.right = TreeNode(7)
return root
def test_binary_tree_problems():
"""Test all binary tree problems with examples."""
print("=== BINARY TREE PROBLEMS & SOLUTIONS ===\n")
# Create test tree
root = create_test_tree()
# Test 1: Tree Traversals
print("1. BINARY TREE TRAVERSALS")
print(f"Inorder: {inorder_traversal(root)}")
print(f"Preorder: {preorder_traversal(root)}")
print(f"Postorder: {postorder_traversal(root)}")
print(f"Level order: {level_order(root)}")
print(f"Zigzag level order: {zigzag_level_order(root)}")
# Test 2: Maximum Depth
print("\n2. MAXIMUM DEPTH")
print(f"Max depth: {max_depth(root)}")
# Test 3: Validate BST
print("\n3. VALIDATE BST")
bst = TreeNode(2)
bst.left = TreeNode(1)
bst.right = TreeNode(3)
print(f"Is valid BST: {is_valid_bst(bst)}")
# Test 4: Same Tree
print("\n4. SAME TREE")
root2 = create_test_tree()
print(f"Trees are same: {is_same_tree(root, root2)}")
# Test 5: Symmetric Tree
print("\n5. SYMMETRIC TREE")
sym_root = TreeNode(1)
sym_root.left = TreeNode(2)
sym_root.right = TreeNode(2)
sym_root.left.left = TreeNode(3)
sym_root.left.right = TreeNode(4)
sym_root.right.left = TreeNode(4)
sym_root.right.right = TreeNode(3)
print(f"Is symmetric: {is_symmetric(sym_root)}")
# Test 6: Binary Tree Paths
print("\n6. BINARY TREE PATHS")
small_tree = TreeNode(1)
small_tree.left = TreeNode(2)
small_tree.right = TreeNode(3)
small_tree.left.right = TreeNode(5)
print(f"All paths: {binary_tree_paths(small_tree)}")
# Test 7: Diameter
print("\n7. DIAMETER OF BINARY TREE")
print(f"Diameter: {diameter_of_binary_tree(root)}")
# Test 8: Maximum Path Sum
print("\n8. MAXIMUM PATH SUM")
print(f"Max path sum: {max_path_sum(root)}")
# Test 9: Right Side View
print("\n9. RIGHT SIDE VIEW")
print(f"Right side view: {right_side_view(root)}")
# Test 10: Serialize/Deserialize
print("\n10. SERIALIZE/DESERIALIZE")
serialized = serialize(root)
print(f"Serialized: {serialized}")
deserialized = deserialize(serialized)
print(f"Deserialized same as original: {is_same_tree(root, deserialized)}")
if __name__ == "__main__":
test_binary_tree_problems()