Geeksforgeeks

Error

×

Given a Binary Search Tree. Find sum of all elements smaller than and equal to Kth smallest element.

**Example 1:**

**Input: **
20
/ \
8 22
/ \
4 12
/ \
10 14 , K=3
**Output:** 22
**Explanation:**
Sum of 3 smallest elements are:
4 + 8 + 10 = 22

**Input:**
10
/ \
5 11
/ \
4 7
\
8 , K=2
**Output:** 9
**Explanation:**
The sum of two smallest elements
is 4+5=9.

You don't need to read input or print anything. Your task is to complete the function **sum() **which takes the **root node** and an **integer K** as input parameters and returns the sum of all elements smaller than and equal to kth smallest element.

1<=Number of nodes in BST<=100

1<=K<=N

We are replacing the old Disqus forum with the new Discussions section given below.

Click here to view old Disqus comments.

Click here to view old Disqus comments.

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

YesLoading...

Sum of k smallest elements in BST

...