Geeksforgeeks

Timer is Running

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. (**ignore duplicate during insertion in 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

22

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

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.

jainshubham52535 | 529 |

Pathak1899 | 436 |

yashdip123 | 380 |

kirtidee18 | 377 |

jots62 | 351 |

kirtidee18 | 1057 |

ykaushik | 752 |

aggarwalvatsal5 | 741 |

harbilassingh | 576 |

pmr_3689 | 570 |

blackshadows | 5456 |

Ibrahim Nash | 5450 |

akhayrutdinov | 5111 |

mb1973 | 4976 |

Quandray | 4827 |

Login to report an issue on this page.