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

# Range Sum of BST Python Solution with Tests

> Tested Python solution for LeetCode 938 with 12 pytest cases. Generate a practice environment with lcpy.

LeetCode 938, [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 Tree](/catalog/topics/binary-tree). [View on LeetCode](https://leetcode.com/problems/range-sum-of-bst/description/).

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

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

## Problem

\<p>Given the \<code>root\</code> node of a binary search tree and two integers \<code>low\</code> and \<code>high\</code>, return \<em>the sum of values of all nodes with a value in the \<strong>inclusive\</strong> range \</em>\<code>\[low, high]\</code>.\</p>

### Examples

![Example 1](https://assets.leetcode.com/uploads/2020/11/05/bst1.jpg)

```
Input: root = [10,5,15,3,7,null,18], low = 7, high = 15
Output: 32
Explanation: Nodes 7, 10, and 15 are in the range [7, 15]. 7 + 10 + 15 = 32.
```

![Example 2](https://assets.leetcode.com/uploads/2020/11/05/bst2.jpg)

```
Input: root = [10,5,15,3,7,13,18,1,null,6], low = 6, high = 10
Output: 23
Explanation: Nodes 6, 7, and 10 are in the range [6, 10]. 6 + 7 + 10 = 23.
```

### Constraints

\<ul>
\<li>The number of nodes in the tree is in the range \<code>\[1, 2 \* 10^4]\</code>.\</li>
\<li>\<code>1 \<= Node.val \<= 10^5\</code>\</li>
\<li>\<code>1 \<= low \<= high \<= 10^5\</code>\</li>
\<li>All \<code>Node.val\</code> are \<strong>unique\</strong>.\</li>
\</ul>

## Solution

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

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


class Solution:
    # Time: O(n)
    # Space: O(h) where h is the tree height
    def range_sum_bst(self, root: TreeNode[int] | None, low: int, high: int) -> int:
        if root is None:
            return 0
        if root.val < low:
            # Entire left subtree is below the range
            return self.range_sum_bst(root.right, low, high)
        if root.val > high:
            # Entire right subtree is above the range
            return self.range_sum_bst(root.left, low, high)
        return (
            root.val
            + self.range_sum_bst(root.left, low, high)
            + self.range_sum_bst(root.right, low, high)
        )
```

## Complexity

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

## Tags

[NeetCode All](/catalog/neetcode).


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