Skip to content
Related Articles

Related Articles

Data Structures | Binary Search Trees | Question 12

Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 16 Nov, 2018
Improve Article
Save Article

Consider the same code as given in above question. What does the function print() do in general?

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);
        if (count == k)
          printf("%d ", root->data);
       print(root->left, k);

(A) Prints the kth smallest element in BST
(B) Prints the kth largest element in BST
(C) Prints the leftmost node at level k from root
(D) Prints the rightmost node at level k from root

Answer: (B)

Explanation: The function basically does reverse inorder traversal of the given Binary Search Tree. The reverse inorder traversal produces data in reverse sorted order. Whenever a nod is visited, count is incremented by 1 and data of a node is printed only when count becomes k.

Quiz of this Question

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!