-
Notifications
You must be signed in to change notification settings - Fork 100
Expand file tree
/
Copy pathlowestCommonAncestor.py
More file actions
79 lines (62 loc) · 1.75 KB
/
lowestCommonAncestor.py
File metadata and controls
79 lines (62 loc) · 1.75 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
class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
# Inorder traversal (Left → Root → Right)
def inorder(p):
if p:
inorder(p.left)
print(p.data, end=", ")
inorder(p.right)
# Find Lowest Common Ancestor (LCA)
def lca(root, n1, n2):
if root is None:
return None
if root.data == n1 or root.data == n2:
return root
left = lca(root.left, n1, n2)
right = lca(root.right, n1, n2)
if left and right:
return root
return left if left else right
# Build the tree manually
def build_sample_tree():
root = Node(3)
root.left = Node(6)
root.right = Node(8)
root.left.left = Node(2)
root.left.right = Node(11)
root.left.right.left = Node(9)
root.left.right.right = Node(5)
root.right.right = Node(13)
root.right.right.left = Node(7)
return root
if __name__ == "__main__":
# Build tree and define queries
root = build_sample_tree()
queries = [(2, 5), (9, 5)]
# Print the input setup
print("Binary Tree Structure:")
print(" 3")
print(" / \\")
print(" 6 8")
print(" / \\ \\")
print(" 2 11 13")
print(" / \\ /")
print(" 9 5 7\n")
print("Queries:")
for n1, n2 in queries:
print(f" n1 = {n1}, n2 = {n2}")
print()
# Print inorder traversal
print("In-Order Traversal: ", end="")
inorder(root)
print("\n")
# Run LCA for each query
for n1, n2 in queries:
result = lca(root, n1, n2)
if result:
print(f"Lowest Common Ancestor of {n1} and {n2}: {result.data}")
else:
print(f"Nodes {n1} and/or {n2} not found.")