Question: / * * * Finds and retrieves the k - largest elements from the BST in sorted order, * least to greatest. * * This

/**
* Finds and retrieves the k-largest elements from the BST in sorted order,
* least to greatest.
*
* This must be done recursively.
*
* In most cases, this method will not need to traverse the entire tree to
* function properly, so you should only traverse the branches of the tree
* necessary to get the data and only do so once. Failure to do so will
* result in an efficiency penalty.
*
* EXAMPLE: Given the BST below composed of Integers:
*
*50
*/\
*2575
*/\
*1237
*/\\
*101540
*/
*13
*
* kLargest(5) should return the list [25,37,40,50,75].
* kLargest(3) should return the list [40,50,75].
*
* Should have a running time of O(log(n)+ k) for a balanced tree and a
* worst case of O(n + k), with n being the number of data in the BST
*
* @param k the number of largest elements to return
* @return sorted list consisting of the k largest elements
* @throws java.lang.IllegalArgumentException if k <0 or k > size
*/
public List kLargest(int k){
* Not allowed to use val *

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!