This function searches for a node with value val in the BST and returns True if found, False otherwise
(root: Optional[Node], val: int)
| 3 | |
| 4 | |
| 5 | def search(root: Optional[Node], val: int) -> bool: |
| 6 | """This function searches for a node with value val in the BST and returns True if found, False otherwise""" |
| 7 | |
| 8 | # If the tree is empty, return False |
| 9 | if root is None: |
| 10 | return False |
| 11 | |
| 12 | # If the root value is equal to the value to be searched, return True |
| 13 | if root.data == val: |
| 14 | return True |
| 15 | |
| 16 | # If the value to be searched is less than the root value, search in the left subtree |
| 17 | if root.data > val: |
| 18 | return search(root.left, val) |
| 19 | return search(root.right, val) |