# 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.
{"ft_lang_label":"__label__en","ft_lang_prob":0.65719336,"math_prob":0.8614283,"size":2204,"snap":"2020-45-2020-50","text_gpt3_token_len":513,"char_repetition_ratio":0.29272726,"word_repetition_ratio":0.44501278,"special_character_ratio":0.25816697,"punctuation_ratio":0.06896552,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9848892,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-10-29T08:01:21Z\",\"WARC-Record-ID\":\"<urn:uuid:f17b4ed5-fd9b-4af1-b6f1-48406596bbe5>\",\"Content-Length\":\"96822\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:e85f9102-4b88-4131-a231-41e136c8ef9d>\",\"WARC-Concurrent-To\":\"<urn:uuid:09d54fc9-2ea7-40b4-9531-a073ee01f1d9>\",\"WARC-IP-Address\":\"104.96.221.200\",\"WARC-Target-URI\":\"https://www.geeksforgeeks.org/data-structures-binary-search-trees-question-12/?ref=lbp\",\"WARC-Payload-Digest\":\"sha1:GVU5BUQUEQYXDEZ2VUK7737X5JW63LRC\",\"WARC-Block-Digest\":\"sha1:FDIN77Y4WZTPPLVKUYEPXEQ2NEU5NGJ6\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-45/CC-MAIN-2020-45_segments_1603107903419.77_warc_CC-MAIN-20201029065424-20201029095424-00318.warc.gz\"}"}