Range Sum of BST
LeetCode 975 | Difficulty: Easyβ
EasyProblem Descriptionβ
Given the root node of a binary search tree and two integers low and high, return the sum of values of all nodes with a value in the inclusive range [low, high].
Example 1:

Input: root = [10,5,15,3,7,null,18], low = 7, high = 15
Output: 32
Explanation: Nodes 7, 10, and 15 are in the range [7, 15]. 7 + 10 + 15 = 32.
Example 2:

Input: root = [10,5,15,3,7,13,18,1,null,6], low = 6, high = 10
Output: 23
Explanation: Nodes 6, 7, and 10 are in the range [6, 10]. 6 + 7 + 10 = 23.
Constraints:
- The number of nodes in the tree is in the range `[1, 2 * 10^4]`.
- `1 <= Node.val <= 10^5`
- `1 <= low <= high <= 10^5`
- All `Node.val` are **unique**.
Topics: Tree, Depth-First Search, Binary Search Tree, Binary Tree
Approachβ
Tree DFSβ
Traverse the tree recursively (or with a stack). At each node, decide: what information do I need from the left/right subtrees? Process: go left β go right β combine results. Consider preorder, inorder, or postorder traversal based on when you need to process the node.
When to use
Path problems, subtree properties, tree structure manipulation.
Solutionsβ
Solution 1: C# (Best: 184 ms)β
| Metric | Value |
|---|---|
| Runtime | 184 ms |
| Memory | 46.7 MB |
| Date | 2022-01-03 |
Solution
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
public class Solution {
public int result = 0;
public int RangeSumBST(TreeNode root, int low, int high) {
if(root == null) return 0;
int val = root.val;
if(val>=low && val <= high) result += val;;
RangeSumBST(root.left, low, high);
RangeSumBST(root.right, low, high);
return result;
}
}
π 3 more C# submission(s)
Submission (2022-01-03) β 184 ms, 46.8 MBβ
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
public class Solution {
public int RangeSumBST(TreeNode root, int low, int high) {
int[] result = new int[1];
RangeSumBSTHelper(root, low, high, result);
return result[0];
}
public void RangeSumBSTHelper(TreeNode root, int low, int high, int[] result)
{
if(root == null) return;
int val = root.val;
if(val>=low && val <= high) result[0] += val;;
RangeSumBSTHelper(root.left, low, high, result);
RangeSumBSTHelper(root.right, low, high, result);
}
}
Submission (2022-01-03) β 192 ms, 46.7 MBβ
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
public class Solution {
public int RangeSumBST(TreeNode root, int low, int high) {
int[] result = new int[1];
RangeSumBSTHelper(root, low, high, result);
return result[0];
}
public void RangeSumBSTHelper(TreeNode root, int low, int high, int[] result)
{
if(root == null) return;
int val = root.val;
if(val>=low && val <= high) result[0] += val;;
if(val >= low)
RangeSumBSTHelper(root.left, low, high, result);
if(val <= high)
RangeSumBSTHelper(root.right, low, high, result);
}
}
Submission (2022-01-03) β 356 ms, 46.6 MBβ
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
public class Solution {
public int result = 0;
public int RangeSumBST(TreeNode root, int low, int high) {
if(root == null) return 0;
int val = root.val;
if(val>=low && val <= high) result += val;;
if(val>=low)
RangeSumBST(root.left, low, high);
if(val <= high)
RangeSumBST(root.right, low, high);
return result;
}
}
Complexity Analysisβ
| Approach | Time | Space |
|---|---|---|
| Tree Traversal | $O(n)$ | $O(h)$ |
Interview Tipsβ
Key Points
- Start by clarifying edge cases: empty input, single element, all duplicates.
- Consider: "What information do I need from each subtree?" β this defines your recursive return value.