 Sum of k smallest elements in BST
Easy Accuracy: 65.96% Submissions: 1334 Points: 2

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
Example 2:
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.

Expected Time Complexity: O(K)
Expected Auxiliary Space: O(1)

Constraints:
1<=Number of nodes in BST<=100
1<=K<=N

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

### Editorial

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

#### My Submissions:  