leetcode/0783_search-in-a-binary-search-tree/python3/solution.py

20 lines
586 B
Python

# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def searchBST(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]:
current = root
while current is not None:
if val == current.val:
return current
elif val < current.val:
current = current.left
else:
current = current.right
return None