Open In App

Data Structures | Binary Search Trees | Question 12

Like Article
Like
Save
Share
Report

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


Last Updated : 28 Jun, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads