Skip to content

Instantly share code, notes, and snippets.

@kartikkukreja
Last active September 15, 2015 03:59
Show Gist options
  • Save kartikkukreja/904f516dc13e610fc05b to your computer and use it in GitHub Desktop.
Save kartikkukreja/904f516dc13e610fc05b to your computer and use it in GitHub Desktop.
2Sum problem in BST
def twoSum(bst, k):
if bst is None: return None
bstStart = inorderFromStart(bst)
bstEnd = inorderFromEnd(bst)
start, end = bstStart.next(), bstEnd.next()
while start != end:
if start.data + end.data == k: return (start.data, end.data)
elif start.data + end.data > k: end = bstEnd.next()
else: start = bstStart.next()
return None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment