AlgoMaster Logo

Range Sum of BST

easyFrequency7 min readUpdated June 23, 2026

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.

This is a BST, not an arbitrary 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. That ordering lets us prune entire subtrees that cannot contain values in our range. If a node's value is below low, its left subtree holds only smaller values, none of which qualify. If a node's value is above high, its right subtree holds only larger values.

Key Constraints:

  • Number of nodes up to 2 * 10^4: An O(n) traversal of the whole tree is acceptable. We do not need a sub-linear solution.
  • 1 <= Node.val <= 10^5: All values are positive, and the maximum possible sum is 2 10^4 10^5 = 2 10^9. This is below the 32-bit signed integer limit of about 2.147 10^9, so a standard int accumulator does not overflow.
  • All Node.val are unique, so each node contributes at most once to the sum.

Approach 1: DFS (Visit All Nodes)

Intuition

Traverse the entire tree with DFS and, at each node, check whether its value falls within [low, high]. If it does, add it to a running sum. This ignores the BST property and treats the tree as a generic binary tree, which means it works on any binary tree, not only a BST.

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 visits every node, including subtrees that cannot contain in-range values. The next approach uses the BST ordering to skip those subtrees.

Approach 2: DFS with BST Pruning (Optimal)

Intuition

The BST property lets us skip subtrees instead of visiting them. For any node:

  • If node.val < low, then this node and everything in its left subtree is below the range, so we recurse only into the right subtree.
  • If node.val > high, then this node and everything in its right subtree is above the range, so we recurse only into the left subtree.
  • If low <= node.val <= high, the node is in range. We add its value and recurse into both subtrees, since the left subtree may hold values at least low and the right subtree may hold values at most high.

Pruning skips entire branches that cannot contribute to the sum. On a balanced BST with a narrow range, the number of visited nodes drops from O(n) toward O(log n + k), where k is the count of in-range values.

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 recursion depth equals the tree height, so a deeply skewed tree can reach a recursion depth of n. The next approach applies the same pruning iteratively with an explicit queue, which keeps the stack depth constant.

Approach 3: Iterative BFS with Pruning

Intuition

The same pruning logic works iteratively with a queue (BFS). We process nodes from the queue, add a value when it is in range, and enqueue a child only when its subtree could contain in-range values.

The decision to enqueue mirrors the recursive pruning. We enqueue the left child only when node.val > low, because if node.val <= low the entire left subtree is at most node.val and below the range. We enqueue the right child only when node.val < high, by the symmetric argument. For the constraints here (up to 2 * 10^4 nodes) recursion would not overflow the stack, but the iterative form keeps stack depth constant regardless of tree shape.

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