> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Search in a Binary Search Tree Python Solution

> Tested Python solution for LeetCode 700 with 24 pytest cases. Generate a practice environment with lcpy.

LeetCode 700, [Easy](/catalog/easy). Topics: [Tree](/catalog/topics/tree), [Binary Search Tree](/catalog/topics/binary-search-tree), [Binary Tree](/catalog/topics/binary-tree). [View on LeetCode](https://leetcode.com/problems/search-in-a-binary-search-tree/description/).

Generate this problem as a practice environment: tested reference solution, 24 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 700   # by problem number
lcpy gen -s search_in_a_binary_search_tree   # by problem name
```

## Problem

You are given the `root` of a binary search tree (BST) and an integer `val`.

Find the node in the BST that the node's value equals `val` and return the subtree rooted with that node. If such a node does not exist, return `null`.

### Examples

![Example 1](https://assets.leetcode.com/uploads/2021/01/12/tree1.jpg)

```
Input: root = [4,2,7,1,3], val = 2
Output: [2,1,3]
```

![Example 2](https://assets.leetcode.com/uploads/2021/01/12/tree2.jpg)

```
Input: root = [4,2,7,1,3], val = 5
Output: []
```

### Constraints

* The number of nodes in the tree is in the range \[1, 5000].
* 1 \<= Node.val \<= 10^7
* root is a binary search tree.
* 1 \<= val \<= 10^7

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/search_in_a_binary_search_tree/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/search_in_a_binary_search_tree/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from leetcode_py import TreeNode


class Solution:
    # Time: O(h) where h is the tree height
    # Space: O(1)
    def search_bst(self, root: TreeNode[int] | None, val: int) -> TreeNode[int] | None:
        node = root
        while node is not None:
            if val == node.val:
                return node
            node = node.left if val < node.val else node.right
        return None
```

## Complexity

| Time | Space |
| - | - |
| O(h) where h is the tree height | O(1) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.