Data Structures | Binary Search Trees | Question 12
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); } } |
chevron_right
filter_none
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
Recommended Posts:
- Data Structures | Binary Search Trees | Question 1
- Data Structures | Binary Search Trees | Question 2
- Data Structures | Binary Search Trees | Question 4
- Data Structures | Binary Search Trees | Question 5
- Data Structures | Binary Search Trees | Question 7
- Data Structures | Binary Search Trees | Question 6
- Data Structures | Binary Search Trees | Question 12
- Data Structures | Binary Search Trees | Question 3
- Data Structures | Binary Search Trees | Question 8
- Data Structures | Binary Search Trees | Question 12
- Data Structures | Binary Search Trees | Question 10
- Data Structures | Balanced Binary Search Trees | Question 7
- Data Structures | Balanced Binary Search Trees | Question 5
- Data Structures | Balanced Binary Search Trees | Question 4
- Data Structures | Balanced Binary Search Trees | Question 9



