-
Notifications
You must be signed in to change notification settings - Fork 434
Expand file tree
/
Copy pathP06_LCAinBinaryTree.py
More file actions
48 lines (36 loc) · 1016 Bytes
/
P06_LCAinBinaryTree.py
File metadata and controls
48 lines (36 loc) · 1016 Bytes
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
# LCA - least common ancestor
# The lowest common ancestor of two nodes n1 and n2 is the lowest node in tree that has both n1 and n2 as descendants.
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 or root.key == 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
# Consider the following binary tree
# 3
# / \
# 6 8
# / \ \
# 2 11 13
# / \ /
# 9 5 7
# Create binary tree
root=node(3)
l = root.left = node(6)
r = root.right = node(8)
r.right = node(13)
r.right.left = node(7)
l.left = node(2)
l.right = node(11)
l.right.left = node(9)
l.right.right = node(5)
print(lca(root, 2, 5).key) # outputs '6'