Sum of k smallest elements in BST
Submissions: 5090   Accuracy:

25.1%

  Difficulty: Easy   Marks: 2

Given Binary Search Tree. The task is to find sum of all elements smaller than and equal to Kth smallest element.
Input:
The first line of input contains a single integer T denoting the number of test cases. Then T test cases follow. Each test case consists of three lines.
First line of each test case consist of integer N, denoting the number of elements in a BST.
Second line of each test case consists of N space separated integers denoting the elements in the BST.
Third line of each test case consists of an integer K, denoting the Kth smallest elements.

Output:
It should be single line output, Print the respective output in the respective line.

Constraints:
1<=T<=100
1<=N<=50
1<=K<=N

Example:
Input:

1
7
20 8 4 12 10 14 22
3
Output:
22
Explanation:
The tree for above input will look like this:
          20
        /     \
       8     22
     /     \
    4     12
         /     \
        10    14
Sum of 3 smallest elements are: 4 + 8 + 10 = 22

** For More Input/Output Examples Use 'Expected Output' option **

Contributor: Saksham Raj Seth
Author: saksham seth


If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.



Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.


to report an issue on this page.