Created
September 18, 2020 09:39
-
-
Save iamprayush/84e104755b9c86ce484ac7cc50006617 to your computer and use it in GitHub Desktop.
Search in a Binary Search Tree
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
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