Problem Description:
Given a Binary Search Tree (BST) and a range [low, high]
, you need to find the sum of all node values within that range.
In a Binary Search Tree (BST):
- The left subtree of any node contains values less than the node's value.
- The right subtree of any node contains values greater than the node's value.
Using these properties, you can skip entire subtrees if their values are not within the given range, making the algorithm more efficient.
-
Base Case:
If the current node isnull
, return0
because an empty tree contributes0
to the sum. -
Check Current Node:
- If the current node's value is within the range
[low, high]
, add its value to the sum.
- If the current node's value is within the range
-
Recursive Traversal:
- Left Subtree: If the current node's value is greater than the
low
bound, recursively traverse the left subtree because there might be values within the range. - Right Subtree: If the current node's value is less than the
high
bound, recursively traverse the right subtree because there might be values within the range.
- Left Subtree: If the current node's value is greater than the
-
Return Sum:
Return the accumulated sum of all nodes that fall within the specified range.
- O(N) where
N
is the number of nodes in the BST.
Each node is visited at most once, but unnecessary subtrees are skipped based on the value range, which can reduce the effective number of nodes visited in practice.
- O(H) where
H
is the height of the tree.
This space is used for the recursive call stack. In the worst case, the height of the tree can be as large asN
(if the tree is skewed), so the space complexity can beO(N)
in the worst case. For a balanced tree, it isO(log N)
.
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public int rangeSumBST(TreeNode root, int low, int high) {
if (root == null) return 0; // Base case: if the current node is null, return 0
int sum = 0; // Initialize sum
// If the current node's value is within the range, add it to the sum
if (root.val >= low && root.val <= high) {
sum += root.val;
}
// If the current node's value is greater than the lower bound,
// there might be valid values in the left subtree
if (root.val > low) {
sum += rangeSumBST(root.left, low, high);
}
// If the current node's value is less than the upper bound,
// there might be valid values in the right subtree
if (root.val < high) {
sum += rangeSumBST(root.right, low, high);
}
return sum; // Return the total sum of all nodes within the range
}
}