-
Notifications
You must be signed in to change notification settings - Fork 436
/
Copy pathP07_LCAinBST.py
43 lines (34 loc) · 861 Bytes
/
P07_LCAinBST.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
# Lowest Common Ancestor in Binary search tree
class node:
def __init__(self,key):
self.key=key
self.left=None
self.right=None
def lca(root,n1,n2):
if root is None:
return None
if root.key<n1 and root.key<n2:
return lca(root.right,n1,n2)
if root.key>n1 and root.key>n2:
return lca(root.left,n1,n2)
return root
# Consider the following BST
# 8
# / \
# 3 11
# / \ / \
# 2 6 10 13
# / \ /
# 5 7 12
# Create BST
root = node(8)
l = root.left = node(3)
r = root.right = node(11)
r.left = node(10)
r.right = node(13)
r.right.left = node(12)
l.left = node(2)
l.right = node(6)
l.right.left = node(5)
l.right.right = node(7)
print(lca(root,2,7).key) # ouputs '3'