Skip to content
Related Articles

Related Articles

Data Structures | Binary Search Trees | Question 12
  • Difficulty Level : Hard
  • Last Updated : 12 Mar, 2013

Consider the following code snippet in C. The function print() receives root of a Binary Search Tree (BST) and a positive integer k as arguments.




// A BST node
struct node {
    int data;
    struct node *left, *right;
};
  
int count = 0;
  
void print(struct node *root, int k)
{
    if (root != NULL && count <= k)
    {
        print(root->right, k);
        count++;
        if (count == k)
          printf("%d ", root->data);
       print(root->left, k);
    }
}

What is the output of print(root, 3) where root represent root of the following BST.

                   15
                /     \
              10      20
             / \     /  \
            8  12   16  25   

(A) 10
(B) 16
(C) 20
(D) 20 10


Answer: (B)

Explanation: The code mainly finds out k’th largest element in BST, see K’th Largest Element in BST for details.

Quiz of this Question

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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :