700. Search in a Binary Search Tree
Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 82% Topics: Tree, Binary Search Tree, Binary Tree
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# 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
Solution from kamyu104/LeetCode-Solutions · MIT