Skip to content

Instantly share code, notes, and snippets.

@wenweixu
Created September 1, 2019 23:47
Show Gist options
  • Save wenweixu/338edb13993a2a3021705a81a445d824 to your computer and use it in GitHub Desktop.
Save wenweixu/338edb13993a2a3021705a81a445d824 to your computer and use it in GitHub Desktop.
Hackerrank Binary Search Tree Lowest Common Ancestor Python solution
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
@Kuldeep708
Copy link

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment