Skip to content

Instantly share code, notes, and snippets.

@iamprayush
Created September 18, 2020 09:39
Show Gist options
  • Save iamprayush/84e104755b9c86ce484ac7cc50006617 to your computer and use it in GitHub Desktop.
Save iamprayush/84e104755b9c86ce484ac7cc50006617 to your computer and use it in GitHub Desktop.
Search in a Binary Search Tree
class Solution:
def searchBST(self, root: TreeNode, val: int) -> TreeNode:
if not root:
return None
if root.val == val:
return root
if root.val > val:
# Current val is too high so move left so we get lower vals.
return self.searchBST(root.left, val)
# Otherwise move right so we get higher vals.
return self.searchBST(root.right, val)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment