Given the root of a binary search tree (BST), and integers low and high, find the sum of all node values in the BST that are within the range [low, high].

Sample Input:

BST:
        10
       / \
      5   15
     / \    \
    3   7    18

Range: [7, 15]

Expected Output:

32

In this example, the BST contains the values [3, 5, 7, 10, 15, 18]. The sum of node values that fall within the range [7, 15] is 32 (7 + 10 + 15).

Time and Space Complexity:


The time complexity of the algorithm is O(n), where ‘n’ is the number of nodes in the BST. We need to visit each node once to calculate the sum of values in the given range. The space complexity is O(h), where ‘h’ is the height of the BST. In the worst case, the recursion stack can grow to the height of the tree.

C++ solution:

#include <iostream>

// Definition for a binary tree node.
struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};

int rangeSumBST(TreeNode* root, int low, int high) {
    // Base case: if the root is null, return 0
    if (root == nullptr) {
        return 0;
    }

    // Initialize the sum to 0
    int sum = 0;

    // If the current node value falls within the range, add it to the sum
    if (root->val >= low && root->val <= high) {
        sum += root->val;
    }

    // If the current node value is greater than the low value, explore the left subtree
    if (root->val > low) {
        sum += rangeSumBST(root->left, low, high);
    }

    // If the current node value is less than the high value, explore the right subtree
    if (root->val < high) {
        sum += rangeSumBST(root->right, low, high);
    }

    return sum;
}

int main() {
    // Construct the binary search tree for the sample input
    TreeNode* root = new TreeNode(10);
    root->left = new TreeNode(5);
    root->right = new TreeNode(15);
    root->left->left = new TreeNode(3);
    root->left->right = new TreeNode(7);
    root->right->right = new TreeNode(18);

    int low = 7;
    int high = 15;

    // Find the sum of node values within the given range
    int sum = rangeSumBST(root, low, high);

    std::cout << "The sum of node values within the range [" << low << ", " << high << "] is: " << sum << std::endl;

    // Don't forget to free the allocated memory
    // ...

    return 0;
}

The rangeSumBST function recursively calculates the sum of node values within the given range [low, high] in the BST. It starts from the root node and checks if the current node value falls within the range. If it does, it adds the current node value to the sum. Then, it explores the left and right subtrees, but only if the current node value is within the range of [low, high].

In the given example, the function will correctly find the sum of node values within the range [7, 15] as 32, and the output will be 32.

As mentioned earlier, the time complexity is O(n) as we need to visit each node once, and the space complexity is O(h) due to the recursion stack, where ‘h’ is the height of the BST.