Last active
September 15, 2015 03:59
-
-
Save kartikkukreja/904f516dc13e610fc05b to your computer and use it in GitHub Desktop.
2Sum problem in BST
This file contains hidden or 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 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