Skip to main content
LeetCode 938, Easy. Topics: Tree, Depth-First Search, Binary Search Tree, Binary Tree. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 12 parametrized pytest cases, and a playground notebook:

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
Example 2

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, full suite in test_solution.py:

Complexity

Tags

NeetCode All.
Last modified on September 7, 2026