Easy
Search in a Binary Search Tree — Python
Full explanation · Time O(h) · Space O(1)
# Time: O(h)
# Space: O(1)
class TreeNode(object):
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution(object):
def searchBST(self, root, val):
"""
:type root: TreeNode
:type val: int
:rtype: TreeNode
"""
while root and val != root.val:
if val < root.val:
root = root.left
else:
root = root.right
return root