Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create P06_LCAinBinaryTree.py #42

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions Trees/P06_LCAinBinaryTree.py
Original file line number Diff line number Diff line change
@@ -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'