Skip to content

Instantly share code, notes, and snippets.

@mailpraveens
Created March 20, 2014 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mailpraveens/9664638 to your computer and use it in GitHub Desktop.
Save mailpraveens/9664638 to your computer and use it in GitHub Desktop.
Find the Lowest Common Ancestor of Binary Search Tree
def findLCA(node,value1,value2):
if(node == None):
return None
#move to right if values are greater
if(node.value < value1 and node.value < value2):
return findLCA(node.right,value1, value2)
#move to left if values are smaller
if(node.value > value1 and node.value > value2):
return findLCA(node.left,value1, value2)
# Whereever it diverges, return the node as its the LCA
return node
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment