Grokking Data Structures & Algorithms for Coding Interviews
Ask Author
Back to course home

0% completed

Range Sum of BST (easy)
Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Problem Statement

Given a Binary Search Tree (BST) and a range defined by two integers, L and R, calculate the sum of all the values of nodes that fall within this range. The node's value is inclusive within the range if and only if L <= node's value <= R.

Examples:

Example 1:

Input:

Tree: 
   10
  /  \
 5   15
/ \   \
3   7   18
Range: [7, 15]

Expected Output: 32

Justification: The values that fall within the range [7, 15] are 7, 10, and 15. Their sum is 7 + 10 + 15 = 32.

Example 2:

Input:

Tree:
   20
  /  \
 5   25
/ \   
3   10
Range: [3, 10]

.....

.....

.....

Like the course? Get enrolled and start learning!

Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible