Sum Binary-Search-Tree Values inside an Inclusive Interval

Quick Overview

Sum Binary-Search-Tree Values inside an Inclusive Interval. Given a binary-search-tree root and bounds low and high, return the sum of every node value in the inclusive interval [low,high].

Sum Binary-Search-Tree Values inside an Inclusive Interval

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem Given a binary-search-tree root and bounds low and high, return the sum of every node value in the inclusive interval `[low,high]`. ```cpp class Solution { public: int rangeSumBST(TreeNode* root, int low, int high); }; ``` ### Examples **Example 1:** ```text Input: root = [8,3,10,1,6,null,14], low = 3, high = 10 Output: 27 Explanation: Included values are3,6,8,and10, totaling27. ``` **Example 2:** ```text Input: root = [5], low = 6, high = 9 Output: 0 Explanation: The only node is outside the interval. ``` ### Constraints - The tree has1 to `2*10^4` nodes. - `1 <= Node.val <= 10^5`; node values are unique. - `1 <= low <= high <= 10^5`. ### Contract considerations The caller supplies the original TreeNode structure. Example arrays only display the tree; the API is not converted to an array function. ```hint Read the boundary condition BST ordering can rule out a whole subtree once its parent falls beyond a range boundary. ``` ### Boundary reminders The caller supplies the original TreeNode structure. Example arrays only display the tree; the API is not converted to an array function.

Overview: Sum Binary-Search-Tree Values inside an Inclusive Interval. Given a binary-search-tree root and bounds low and high, return the sum of every node value in the inclusive interval [low,high].

Google logo
Google
Jun 23, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Problem

Given a binary-search-tree root and bounds low and high, return the sum of every node value in the inclusive interval [low,high].

class Solution {
public:
    int rangeSumBST(TreeNode* root, int low, int high);
};

Examples

Example 1:

Input: root = [8,3,10,1,6,null,14], low = 3, high = 10
Output: 27
Explanation: Included values are3,6,8,and10, totaling27.

Example 2:

Input: root = [5], low = 6, high = 9
Output: 0
Explanation: The only node is outside the interval.

Constraints

  • The tree has1 to 2*10^4 nodes.
  • 1 <= Node.val <= 10^5 ; node values are unique.
  • 1 <= low <= high <= 10^5 .

Contract considerations

The caller supplies the original TreeNode structure. Example arrays only display the tree; the API is not converted to an array function.

Boundary reminders

The caller supplies the original TreeNode structure. Example arrays only display the tree; the API is not converted to an array function.