# Data Structures | Binary Search Trees | Question 12

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); ` `        ``count++; ` `        ``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

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
Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.