#K79532. Range Sum of BST
Range Sum of BST
Range Sum of BST
You are given a sequence of integers which are inserted into a Binary Search Tree (BST) in the given order. Recall that in a BST, for every node, all keys in the left subtree are smaller than the node's key and all keys in the right subtree are larger.
After constructing the BST, you are provided with two integers, \(L\) and \(R\), representing the lower and upper bounds of a range. Your task is to compute the sum of all nodes in the tree whose keys fall within the inclusive range \([L, R]\).
For example, if the input keys are [10, 5, 15, 3, 7, 18, 2] and the range is [7, 15], then the nodes 7, 10, and 15 are within this range, and their sum is 32.
inputFormat
The first line contains an integer n
, the number of nodes in the BST.
The second line contains n
space-separated integers, representing the keys inserted into the BST in the given order.
The third line contains two space-separated integers, L
and R
, which represent the lower and upper bounds of the range.
outputFormat
Output a single integer representing the sum of all BST nodes with keys in the inclusive range \([L, R]\).
## sample7
10 5 15 3 7 18 2
7 15
32
</p>