-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlcaBinaryTrees.py
More file actions
49 lines (39 loc) · 1.25 KB
/
lcaBinaryTrees.py
File metadata and controls
49 lines (39 loc) · 1.25 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
# LCA for a Binary Tree; not a BST
class Node(object):
def __init__(self, data):
self.data = data
self.left = None
self.right = None
def leastCommonAncestor(root, lNode, rNode):
if root is None:
return None
if root.data == lNode.data or root.data == rNode.data:
return root.data
else:
lcaLeft = leastCommonAncestor(root.left, lNode, rNode)
lcaRight = leastCommonAncestor(root.right, lNode, rNode)
# success because the following means the left node is
# present in left subtree and right in right subtree
if lcaLeft and lcaRight:
return root.data
if lcaLeft:
# means only the left subtree will have the LCA
return lcaLeft
if lcaRight:
# means only the right subtree will have the LCA
return lcaRight
# standalone main
def main():
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)
root.right.left.left = Node(8)
lNode = root.right.right
rNode = root.right.left.left
print(leastCommonAncestor(root, lNode, rNode))
if __name__ == "__main__":
main()