Created
March 20, 2014 14:13
-
-
Save mailpraveens/9664638 to your computer and use it in GitHub Desktop.
Find the Lowest Common Ancestor of Binary Search Tree
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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