From 67fd74ff88dee11e33d88210195dbc1badcb948e Mon Sep 17 00:00:00 2001 From: Naina Gupta <46136786+naina024@users.noreply.github.com> Date: Thu, 1 Oct 2020 23:17:16 +0530 Subject: [PATCH] Create P06_LCAinBinaryTree.py --- Trees/P06_LCAinBinaryTree.py | 48 ++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 Trees/P06_LCAinBinaryTree.py diff --git a/Trees/P06_LCAinBinaryTree.py b/Trees/P06_LCAinBinaryTree.py new file mode 100644 index 0000000..860f2c4 --- /dev/null +++ b/Trees/P06_LCAinBinaryTree.py @@ -0,0 +1,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'