Given Binary Search Tree. The task is to find sum of all elements smaller than and equal to Kth smallest element.
Input : K = 3 8 / \ 7 10 / / \ 2 9 13 Output : 17 Explanation : Kth smallest element is 8 so sum of all element smaller then or equal to 8 are 2 + 7 + 8 Input : K = 5 8 / \ 5 11 / \ 2 7 \ 3 Output : 25
Method 1 (Does not changes BST node structure)
The idea is to traverse BST in inorder traversal. Note that Inorder traversal of BST accesses elements in sorted (or increasing) order. While traversing, we keep track of count of visited Nodes and keep adding Nodes until the count becomes k.
Time complexity : O(k)
Method 2 (Efficient and changes structure of BST)
We can find the required sum in O(h) time where h is height of BST. Idea is similar to Kth-th smallest element in BST . Here we use augmented tree data structure to solve this problem efficiently in O(h) time [ h is height of BST ] .
BST Node contain to extra fields : Lcount , Sum For each Node of BST lCount : store how many left child it has Sum : store sum of all left child it has Find Kth smallest element [ temp_sum store sum of all element less than equal to K ] ksmallestElementSumRec(root, K, temp_sum) IF root -> lCount == K + 1 temp_sum += root->data + root->sum; break; ELSE IF k > root->lCount // Goto right sub-tree temp_sum += root->data + root-> sum; ksmallestElementSumRec(root->right, K-root->lcount+1, temp_sum) ELSE // Goto left sun-tree ksmallestElementSumRec( root->left, K, temp_sum)
Below is implementation of above algo :
Time Complexity: O(h) where h is height of tree.
This article is contributed by Nishant Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find k-th smallest element in BST (Order Statistics in BST)
- Two nodes of a BST are swapped, correct the BST
- K'th Largest Element in BST when modification to BST is not allowed
- K'th smallest element in BST using O(1) Extra Space
- Find the smallest and second smallest elements in an array
- Average of remaining elements after removing K largest and K smallest elements from array
- Convert a BST to a Binary Tree such that sum of all greater keys is added to every key
- Find a pair with given sum in a Balanced BST
- Find a pair with given sum in BST
- A program to check if a binary tree is BST or not
- Find the largest BST subtree in a given Binary Tree | Set 1
- Sorted Array to Balanced BST
- Construct BST from given preorder traversal | Set 2
- Add all greater values to every node in a given BST
- Largest BST in a Binary Tree | Set 2
- Find median of BST in O(n) time and O(1) space
- Shortest distance between two nodes in BST
- K'th Largest element in BST using constant extra space
- k largest(or smallest) elements in an array | added Min Heap method
- k-th smallest absolute difference of two elements in an array