[LeetCode]#700. Search in a Binary Search Tree

Given the tree:
4
/ \
2 7
/ \
1 3
And the value to search: 2
2     
/ \
1 3
class Solution:
def searchBST(self, root, val):
if root is None:
return None

while root:
if root.val == val:
return root
elif root.val > val:
root = root.left
else:
root = root.right

return root

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store