> ## 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.

# Closest Binary Search Tree Value

> Tested Python solution for LeetCode 270 with 15 pytest cases. Generate a practice environment with lcpy.

LeetCode 270, [Easy](/catalog/easy). Topics: [Tree](/catalog/topics/tree), [Depth-First Search](/catalog/topics/depth-first-search), [Binary Search Tree](/catalog/topics/binary-search-tree), [Binary Search](/catalog/topics/binary-search), [Binary Tree](/catalog/topics/binary-tree). [View on LeetCode](https://leetcode.com/problems/closest-bst-value/description/).

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

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

## Problem

Given the `root` of a binary search tree and a `target` value, return *the value in the BST that is closest to the* `target`. If there are multiple answers, print the smallest.

### Examples

![Example 1](https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0200-0299/0270.Closest%20Binary%20Search%20Tree%20Value/images/closest1-1-tree.jpg)

```
Input: root = [4,2,5,1,3], target = 3.714286
Output: 4
```

```
Input: root = [1], target = 4.428571
Output: 1
```

### Constraints

* The number of nodes in the tree is in the range `[1, 10^4]`.
* `0 <= Node.val <= 10^9`
* `-10^9 <= target <= 10^9`

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/closest_bst_value/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/closest_bst_value/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 closest_value(self, root: TreeNode[int], target: float) -> int:
        closest = root.val
        node: TreeNode[int] | None = root
        while node is not None:
            if abs(node.val - target) < abs(closest - target) or (
                abs(node.val - target) == abs(closest - target) and node.val < closest
            ):
                closest = node.val
            node = node.left if target < node.val else node.right
        return closest
```

## Complexity

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

## Tags

[NeetCode All](/catalog/neetcode).


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