AlgoMaster Logo

Range Sum of BST

Last Updated: March 29, 2026

easy

Understanding the Problem

We have a binary search tree (BST) and a range defined by low and high. We need to find every node whose value falls within that range (inclusive on both ends) and return their sum.

The critical detail here is that this is a BST, not just any binary tree. In a BST, for every node, all values in its left subtree are smaller and all values in its right subtree are larger. This ordering property gives us the ability to prune entire subtrees that can't possibly contain values in our range. If a node's value is less than low, there's no point exploring its left subtree since everything there is even smaller. Similarly, if a node's value is greater than high, its right subtree is all larger values we don't care about.

Key Constraints:

  • Number of nodes up to 2 * 10^4 → Even O(n) traversal of the entire tree is perfectly fine. We're not under pressure to find a sub-linear solution.
  • 1 <= Node.val <= 10^5 → All positive values, so no concerns about integer overflow when summing (max possible sum is 2 10^4 10^5 = 2 * 10^9, which fits in a 32-bit signed integer but is close to the limit).
  • 1 <= low <= high <= 10^5 → The range is always valid (low never exceeds high), so no need for input validation.
  • All Node.val are unique → Each node contributes at most once to the sum. No duplicate counting issues.

Approach 1: DFS (Visit All Nodes)

Intuition

The most straightforward approach is to traverse the entire tree using DFS and, at each node, check whether its value falls within [low, high]. If it does, add it to our running sum. This completely ignores the BST property and treats the tree as a generic binary tree.

It's simple, it's correct, and it's easy to code under pressure. For small trees, this is perfectly fine.

Algorithm

  1. Start a DFS traversal from the root.
  2. At each node, check if its value is between low and high (inclusive).
  3. If yes, add the node's value to the sum.
  4. Recurse on both left and right children regardless of the node's value.
  5. Return the total sum.

Example Walkthrough

1Start DFS at root (10). Check if 7 ≤ 10 ≤ 15 → yes, sum = 10
10sum=105371518
1/7

Code

This approach works correctly but visits every node, even those in subtrees that can't possibly contain in-range values. What if we used the BST ordering property to skip entire subtrees?

Approach 2: DFS with BST Pruning (Optimal)

Intuition

The BST property gives us a powerful shortcut. For any node:

  • If node.val < low, then node.val and everything in its left subtree is below our range. We only need to check the right subtree.
  • If node.val > high, then node.val and everything in its right subtree is above our range. We only need to check the left subtree.
  • If low <= node.val <= high, the node is in range. We add its value and need to check both subtrees since the left subtree might have values >= low and the right subtree might have values <= high.

This pruning means we skip entire branches of the tree that can't contribute to the sum. On a balanced BST with a narrow range, this can reduce the work dramatically.

Algorithm

  1. If the current node is null, return 0.
  2. If the node's value is less than low, recurse only on the right child (left subtree is all too small).
  3. If the node's value is greater than high, recurse only on the left child (right subtree is all too large).
  4. Otherwise, add the node's value and recurse on both children.

Example Walkthrough

1Visit 10: 7 ≤ 10 ≤ 15 → in range, add 10. Recurse both children.
10add 105371518
1/6

Code

The recursive approach is clean and concise. But what if we wanted to avoid recursion entirely, perhaps to guard against stack overflow on very deep trees?

Approach 3: Iterative BFS with Pruning

Intuition

The same pruning logic works iteratively with a queue (BFS). We process nodes from the queue, add the value if it's in range, and only enqueue children whose subtrees could contain in-range values.

Some interviewers prefer seeing an iterative solution because it avoids the risk of stack overflow on very deep trees, even though for the given constraints (up to 2 * 10^4 nodes) recursion is perfectly safe.

Algorithm

  1. Initialize a queue with the root node and a sum variable set to 0.
  2. While the queue is not empty, dequeue a node.
  3. If the node's value is in range, add it to the sum.
  4. If the node's value is greater than low, enqueue the left child (it might have in-range values).
  5. If the node's value is less than high, enqueue the right child (it might have in-range values).
  6. Return the sum.

Example Walkthrough

1Initialize queue = [10], sum = 0
10dequeue5371518
1/6

Code