Please use Laptop/Desktop or any other large screen for the Mock Interview Session.

Range Sum of BST



YouTube Video Thumbnail
Link

Watch above sample mock interview video to see how it works.
Login and Buy Premium to Start the Interview



Range Sum in BST Problem

Range Sum in BST

Problem Statement

You are given the root node of a binary search tree and two integers minVal and maxVal. Your task is to find the sum of all node values that lie within the inclusive range [minVal, maxVal] in the tree.

Examples

Example 1:

Input: tree = [12,7,20,4,10,null,25], minVal = 8, maxVal = 20
Output: 42
Explanation: Nodes with values 10, 12, and 20 are within the range [8, 20]. Their sum is 10 + 12 + 20 = 42.
  

Example 2:

Input: tree = [8,3,15,1,6,12,18,null,null,4,7], minVal = 5, maxVal = 15
Output: 50
Explanation: Nodes with values 6, 7, 8, 12, and 15 fall within the range [5, 15]. Their sum is 6 + 7 + 8 + 12 + 15 = 48.
  

Constraints

  • The total number of nodes in the tree is between 1 and 25000.
  • Each node's value is between 1 and 120000.
  • 1 ≤ minVal ≤ maxVal ≤ 120000
  • All node values are distinct.