> For the complete documentation index, see [llms.txt](https://zedive.gitbook.io/project-l/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://zedive.gitbook.io/project-l/part-1/basic_data_structure/binary_tree/binary_search_tree/search-in-a-binary-search-tree.md).

# Search in a Binary Search Tree

## Question ([LC.700](https://leetcode.com/problems/search-in-a-binary-search-tree/))&#x20;

> Given the root of a BST and a target value, find the node with the corresponding value.&#x20;

## Example&#x20;

```
I: Given the root 4, target value is 2 
        4
       / \
      2   7
     / \
    1   3

O: 
      2     
     / \   
    1   3
```

## Analysis&#x20;

Searching a balanced BST is fast. O(logn) time.&#x20;

## Code&#x20;

```python
def searchBST(self, root: TreeNode, val: int) -> TreeNode:
    
    if root is None:
        return None 
    
    if root.val == val:
        return root 
    
    if val >= root.val:
        return self.searchBST(root.right, val)
    else:
        return self.searchBST(root.left, val)

```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://zedive.gitbook.io/project-l/part-1/basic_data_structure/binary_tree/binary_search_tree/search-in-a-binary-search-tree.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
