-
Notifications
You must be signed in to change notification settings - Fork 134
/
Lowest_Common_Ancestor.py
99 lines (76 loc) · 2.37 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
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
""" Program to find LCA of n1 and n2 using one traversal of
Binary tree
"""
# A binary tree node
class Node:
# Constructor to create a new node
def __init__(self, key):
self.key = key
self.left = None
self.right = None
# This function returns reference to LCA of two given values n1 and n2
# v1 is set as true by this function if n1 is found
# v2 is set as true by this function if n2 is found
def findLCAUtil(root, n1, n2, v):
# Base Case
if root is None:
return None
# IF either n1 or n2 matches ith root's key, report
# the presence by setting v1 or v2 as true and return
# root
if root.key == n1:
v[0] = True
return root
if root.key == n2:
v[1] = True
return root
# Look for keys in left and right subtree
left_lca = findLCAUtil(root.left, n1, n2, v)
right_lca = findLCAUtil(root.right, n1, n2, v)
# if one key is present in left subtree and other is present in other,
# So this node is the LCA
if left_lca and right_lca:
return root
# Otherwise check if left subtree or right subtree is LCA
return left_lca if left_lca is not None else right_lca
def find(root, k):
# Base Case
if root is None:
return False
# If key is present at root, or if left subtree or right
# subtree , return true
if (root.key == k or find(root.left, k) or
find(root.right, k)):
return True
# Else return false
return False
# This function returns LCA of n1 and n2 only if both
# n1 and n2 are present in tree, otherwise returns None
def findLCA(root, n1, n2):
# Initialize n1 and n2 as not visited
v = [False, False]
# Find lca of n1 and n2
lca = findLCAUtil(root, n1, n2, v)
# Returns LCA only if both n1 and n2 are present in tree
if v[0] and v[1] or v[0] and find(lca, n2) or v[1] and find(lca, n1):
return lca
# Else return None
return None
# Driver program to test above function
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
root.right.left = Node(6)
root.right.right = Node(7)
lca = findLCA(root, 4, 5)
if lca is not None:
print("LCA(4, 5) = ", lca.key)
else:
print("Keys are not present")
lca = findLCA(root, 4, 10)
if lca is not None:
print("LCA(4,10) = ", lca.key)
else:
print("Keys are not present")