Created
September 1, 2019 23:47
-
-
Save wenweixu/338edb13993a2a3021705a81a445d824 to your computer and use it in GitHub Desktop.
Hackerrank Binary Search Tree Lowest Common Ancestor Python solution
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 lca(root, v1, v2): | |
# compare v1 v2 and get the big and small value | |
if v1 > v2: | |
v_big, v_small = v1, v2 | |
else: | |
v_big, v_small = v2, v1 | |
current = root | |
while True: | |
if current.info > v_big: | |
current = current.left | |
elif current.info < v_small: | |
current = current.right | |
elif current.info >= v_small and current.info <= v_big: | |
return current |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks