-
Notifications
You must be signed in to change notification settings - Fork 65
/
lowest_common_ancestor.py
55 lines (41 loc) · 1.27 KB
/
lowest_common_ancestor.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
""" Lowest Common Ancestor in Binary Tree
"""
class Node:
""" Node contains actual data and address to left and right node """
def __init__(self, data):
self.data = data
self.left = None
self.right = None
def lowest_common_ancestor(root, a, b):
""" lowest common ancestor in BST """
if root is None:
return None
if root.data == a or root.data == b:
return root
left = lowest_common_ancestor(root.left, a, b)
right = lowest_common_ancestor(root.right, a, b)
if left is not None and right is not None:
return root
if left is None and right is None:
return None
if left is not None:
return left
else:
return right
def main():
""" operational function """
root = Node(2)
root.left = Node(7)
root.left.left = Node(21)
root.left.right = Node(6)
root.left.right.left = Node(5)
root.left.right.right = Node(11)
root.right = Node(5)
root.right.right = Node(9)
root.right.right.left = Node(4)
root.right.right.left.right = Node(40)
root.right.right.left.right.right = Node(46)
lca_node = lowest_common_ancestor(root, 21, 11)
print("lowest_common_ancestor for 21 and 11:", lca_node.data)
if __name__ == "__main__":
main()