Given a BST, the task is to find the sum of all elements greater than or equal to K-th largest element in O(1) space.
Input : K = 3 8 / \ 7 10 / / \ 2 9 13 Output : 32 Explanation: 3rd largest element is 9 so sum of all elements greater than or equal to 9 are 9 + 10 + 13 = 32. Input : K = 2 8 / \ 5 11 / \ 2 7 \ 3 Output : 19 Explanation: 2nd largest element is 8 so sum of all elements greater than or equal to 8 are 8 + 11 = 19.
Approach: The approach here is to do reverse inorder traversal, and while doing it simply keep a count of the number of nodes visited. Until the count of visited nodes is less than equal to K, keep on adding the current node’s data. Use the fact that the reverse inorder traversal of a BST gives us a list which is sorted in decreasing order. But recursion or stack/queue based approach to do reverse inorder traversal because both these techniques consume O(n) extra memory, instead make use of Reverse Morris Traversal to do inorder tree traversal, which is a memory efficient and faster method to do reverse inorder tree traversal based on threaded binary trees.
Given below is the algorithm:
1) Initialize Current as root. 2) Initialize a "count" and "sum" variable to 0. 3) While current is not NULL : 3.1) If the current has no right child a) Increment count and check if count is less than or equal to K. 1) Simply add the current node's data in "sum" variable. b) Otherwise, Move to the left child of current. 3.2) Else, here we have 2 cases: a) Find the inorder successor of current Node. Inorder successor is the left most Node in the right subtree or right child itself. b) If the left child of the inorder successor is NULL: 1) Set current as the left child of its inorder successor. 2) Move current Node to its right child. c) Else, if the threaded link between the current Node and it's inorder successor already exists : 1) Set left pointer of the inorder successor as NULL. 2) Increment count and check if the count is less than or equal to K. 2.a) Simply add the current node's data in "sum" variable. 3) Otherwise, Move current to it's left child. 4)After the traversal is complete simply return the sum.
Below is the implementation of the above approach:
Time Complexity: O(N)
Space complexity: O(1)
- Find maximum in a stack in O(1) time and O(1) extra space
- Print BST keys in given Range | O(1) Space
- Counting Triangles in a Rectangular space using BIT
- Maximize the sum of X+Y elements by picking X and Y elements from 1st and 2nd array
- Find the closest element in Binary Search Tree | Space Efficient Method
- Largest Rectangular Area in a Histogram | Set 1
- Range query for Largest Sum Contiguous Subarray
- Sort elements by frequency | Set 5 (using Java Map)
- Count elements which divide all numbers in range L-R
- Count number of smallest elements in given range
- Array range queries for elements with frequency same as value
- Queries for number of distinct elements in a subarray
- Merge Sort Tree (Smaller or equal elements in given row range)
- Number of elements greater than K in the range L to R using Fenwick Tree (Offline queries)
- Number of elements less than or equal to a given number in a given subarray | Set 2 (Including Updates)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.