Sum of BST Nodes Within a Given Range

Last Updated : 11 Oct, 2025

Given the root of a Binary Search Tree containing n nodes, and two integers l and r, find the sum of all node values that lie within the inclusive range [l, r].

Examples:

Input: l = 10, r = 22

print_bst_keys_in_given_range

Output: 54
Explanation: The nodes in the given Tree that lies in the range [10, 22] are {12, 20, 22}. Therefore, the sum of nodes is 12 + 20 + 22 = 54.

Input: l = 11, r = 15

420046700

Output: 11
Explanation: The nodes in the given Tree that lies in the range [11, 15] is {11}. Therefore, the sum of node is 11.

Try It Yourself
redirect icon

[Approach] - Traverse the tree and check if node lies in range

The idea to traverse the tree recursively. For each node, if it lies within the range, then return the sum of left subtree, right subtree and current node. If its value is less than range, then return the value of right subtree, because all the nodes in its left subtree are less than than the range. Otherwise return the value of left subtree, because all the nodes in its right subtree are greater than the range.

C++
//Driver Code Starts
#include <iostream>
using namespace std;

// Node strcuture
class Node {
public:
    int data;
    Node *left, *right;
    Node (int x) {
        data = x;
        left = nullptr;
        right = nullptr;
    }
};
//Driver Code Ends


int nodeSum(Node *root, int l, int r) {
    
    if (root == nullptr) return 0;
    
    // If root value is less than range
    // all nodes in its left subtree 
    // will be less than range
    if (root->data < l) {
        return nodeSum(root->right, l, r);
    }
    
    // If root value is greater than range.
    // all nodes in its right subtree 
    // will be greater than range
    else if (root->data > r) {
        return nodeSum(root->left, l, r);
    }
    
    // If root value lies in the range.
    int left = nodeSum(root->left, l, r);
    int right = nodeSum(root->right, l, r);
    
    return left + right + root->data;
}


//Driver Code Starts
int main() {
    
    // BST
    //       22
    //      /  \
    //    12    30
    //   /  \
    //  8    20
    Node* root = new Node(22);
    root->left = new Node(12);
    root->right = new Node(30);
    root->left->left = new Node(8);
    root->left->right = new Node(20);
    int l = 10, r = 22;
    
    cout << nodeSum(root, l, r) << endl;
    
    return 0;
}
//Driver Code Ends
Java
//Driver Code Starts
import java.util.ArrayList;

// Node strcuture
class Node {
    int data;
    Node left, right;

    Node(int x) {
        data = x;
        left = null;
        right = null;
    }
}

class GFG {
//Driver Code Ends


    static int nodeSum(Node root, int l, int r) {
        if (root == null) return 0;

        // If root value is less than range.
        // all nodes in its left subtree 
        // will be less than range
        if (root.data < l) {
            return nodeSum(root.right, l, r);
        }

        // If root value is greater than range.
        // all nodes in its right subtree 
        // will be greater than range
        else if (root.data > r) {
            return nodeSum(root.left, l, r);
        }

        // If root value lies in the range.
        int left = nodeSum(root.left, l, r);
        int right = nodeSum(root.right, l, r);

        return left + right + root.data;
    }


//Driver Code Starts
    public static void main(String[] args) {

        // BST
        //       22
        //      /  \
        //    12    30
        //   /  \
        //  8    20
        Node root = new Node(22);
        root.left = new Node(12);
        root.right = new Node(30);
        root.left.left = new Node(8);
        root.left.right = new Node(20);
        int l = 10, r = 22;

        System.out.println(nodeSum(root, l, r));
    }
}

//Driver Code Ends
Python
#Driver Code Starts
# Node strcuture
class Node:
    def __init__(self, x):
        self.data = x
        self.left = None
        self.right = None
#Driver Code Ends



def nodeSum(root, l, r):
    if root is None:
        return 0
    
    #  If root value is less than range.
    #  all nodes in its left subtree 
    #  will be less than range
    if root.data < l:
        return nodeSum(root.right, l, r)
    
    #  If root value is greater than range.
    #  all nodes in its right subtree 
    #  will be greater than range
    elif root.data > r:
        return nodeSum(root.left, l, r)
    
    # If root value lies in the range.
    left = nodeSum(root.left, l, r)
    right = nodeSum(root.right, l, r)

    return left + right + root.data

#Driver Code Starts

if __name__ == "__main__":

    # BST
    #       22
    #      /  \
    #    12    30
    #   /  \
    #  8    20
    root = Node(22)
    root.left = Node(12)
    root.right = Node(30)
    root.left.left = Node(8)
    root.left.right = Node(20)
    l = 10
    r = 22

    print(nodeSum(root, l, r))

#Driver Code Ends
C#
//Driver Code Starts
using System;
using System.Collections.Generic;

// Node strcuture
class Node {
    public int data;
    public Node left, right;

    public Node(int x) {
        data = x;
        left = null;
        right = null;
    }
}

class GFG {
//Driver Code Ends

    
    static int nodeSum(Node root, int l, int r) {
        if (root == null) return 0;

        // If root value is less than range.
        // all nodes in its left subtree 
        // will be less than range
        if (root.data < l) {
            return nodeSum(root.right, l, r);
        }

        // If root value is greater than range.
        // all nodes in its right subtree 
        // will be greater than range
        else if (root.data > r) {
            return nodeSum(root.left, l, r);
        }

        // If root value lies in the range.
        int left = nodeSum(root.left, l, r);
        int right = nodeSum(root.right, l, r);

        return left + right + root.data;
    }


//Driver Code Starts
    static void Main(string[] args) {

        // BST
        //       22
        //      /  \
        //    12    30
        //   /  \
        //  8    20
        Node root = new Node(22);
        root.left = new Node(12);
        root.right = new Node(30);
        root.left.left = new Node(8);
        root.left.right = new Node(20);
        int l = 10, r = 22;

        Console.WriteLine(nodeSum(root, l, r));
    }
}

//Driver Code Ends
JavaScript
//Driver Code Starts
// Node strcuture
class Node {
    constructor(x) {
        this.data = x;
        this.left = null;
        this.right = null;
    }
}
//Driver Code Ends


function nodeSum(root, l, r) {
    if (root === null) return 0;

    // If root value is less than range.
    // all nodes in its left subtree 
    // will be less than range
    if (root.data < l) {
        return nodeSum(root.right, l, r);
    }

    // If root value is greater than range.
    // all nodes in its right subtree 
    // will be greater than range
    else if (root.data > r) {
        return nodeSum(root.left, l, r);
    }

    // If root value lies in the range.
    let left = nodeSum(root.left, l, r);
    let right = nodeSum(root.right, l, r);

    return left + right + root.data;
}


//Driver Code Starts
// Driver code
// BST
//       22
//      /  \
//    12    30
//   /  \
//  8    20
let root = new Node(22);
root.left = new Node(12);
root.right = new Node(30);
root.left.left = new Node(8);
root.left.right = new Node(20);
let l = 10, r = 22;

console.log(nodeSum(root, l, r));

//Driver Code Ends

Output
54

Time Complexity: O(n), where n is the number of nodes in tree.
Auxiliary Space: O(h), where h is the height of tree.

Related articles:

Comment