if val is greater than current root value, go right subtree
otherwise, go left
# 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]:
if not root:
return None
if root.val == val:
return root
if root.val < val:
return self.searchBST(root.right, val)
return self.searchBST(root.left, val)
use the recursion to traverse the tree