14. Sum of Nodes in BST Range
β GFG solution to the Sum of Nodes in BST Range problem: efficiently calculate sum of all nodes within a given range using BST properties. π
The problem can be found at the following link: π Question Link
π§© Problem Description
You are given the root of a Binary Search Tree and two integers l
and r
. Your task is to find the sum of all nodes that lie between l
and r
, including both l
and r
.
A Binary Search Tree (BST) is a binary tree where for each node, all values in the left subtree are less than the node's value, and all values in the right subtree are greater than the node's value. This property allows efficient searching and range queries.
π Examples
Example 1
Input: root = [22, 12, 30, 8, 20], l = 10, r = 22
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.
Example 2
Input: root = [8, 5, 11, 3, 6, N, 20], l = 11, r = 15
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.
π Constraints
$0 \le \text{number of nodes} \le 10^4$
$0 \le \text{node->data} \le 10^4$
$0 \le l \le r \le 10^4$
β
My Approach
The optimal approach leverages the Binary Search Tree property to efficiently prune unnecessary branches during traversal:
Optimized DFS with BST Property
Base Case:
If the current node is null, return 0 (no contribution to sum).
Prune Left Subtree:
If the current node's value is less than
l
, all nodes in the left subtree will also be less thanl
.Therefore, skip the left subtree and recursively search only the right subtree.
Prune Right Subtree:
If the current node's value is greater than
r
, all nodes in the right subtree will also be greater thanr
.Therefore, skip the right subtree and recursively search only the left subtree.
Node Within Range:
If the current node's value lies within
[l, r]
, include it in the sum.Recursively calculate sum from both left and right subtrees.
Return the total:
current node value + left subtree sum + right subtree sum
.
Efficiency:
By leveraging BST properties, we avoid visiting nodes that are guaranteed to be outside the range.
This significantly reduces the number of nodes visited compared to a simple tree traversal.
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(n), where n is the number of nodes in the BST. In the worst case (skewed tree or all nodes in range), we visit all nodes. However, on average, the BST property allows us to prune branches and visit only O(h + k) nodes, where h is the height and k is the number of nodes in range.
Expected Auxiliary Space Complexity: O(h), where h is the height of the tree, due to the recursion call stack. In the worst case of a skewed tree, h = n, but for a balanced BST, h = log(n).
π§βπ» Code (C++)
class Solution {
public:
int nodeSum(Node* root, int l, int r) {
if (!root) return 0;
if (root->data < l) return nodeSum(root->right, l, r);
if (root->data > r) return nodeSum(root->left, l, r);
return root->data + nodeSum(root->left, l, r) + nodeSum(root->right, l, r);
}
};
β Code (Java)
class Solution {
public int nodeSum(Node root, int l, int r) {
if (root == null) return 0;
if (root.data < l) return nodeSum(root.right, l, r);
if (root.data > r) return nodeSum(root.left, l, r);
return root.data + nodeSum(root.left, l, r) + nodeSum(root.right, l, r);
}
}
π Code (Python)
class Solution:
def nodeSum(self, root, l, r):
if not root: return 0
if root.data < l: return self.nodeSum(root.right, l, r)
if root.data > r: return self.nodeSum(root.left, l, r)
return root.data + self.nodeSum(root.left, l, r) + self.nodeSum(root.right, l, r)
π§ Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: π¬ Any Questions?. Let's make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated