GATE Computer ScienceVery hardGATE CS (PYQ pattern)
bool f(Node *n) { if (!n) return true; if (n->l && n->l->k > n->k) return false; if (n->r && n->r->k < n->k) return false; return f(n->l) && f(n->r); } The tree as (key, left, right): P = (20, (10, (5), (25)), (30)); Q = (20, (10, -, (15)), (30, (25), (40))); R = (20, (10), (30, (15), -)); S = (20, (10, (5), (12)), (30, (25), (35))). For which of these does f return true although the tree is not a BST?
- A. P and R only
- B. P, Q and R only
- C. R and S only
- D. P only
Correct answer: A. P and R only
f only compares each node with its direct children. P (25 under 10 is in 20's left subtree), R (15 under 30 is in 20's right subtree) pass f but break the BST property; Q and S are genuine BSTs.
Practise more Programming and Data Structures (GATE) questionsProgramming and Data Structures (GATE): notes and key points
More Programming and Data Structures (GATE) questions
- In the BST obtained by inserting 40, 20, 60, 10, 30, 50, 5, 15, 3, 1, 25, 55, 57, 56 in order, how many nodes have a balance factor…
- A binary tree is stored in level order with null children written as : 1 2 3 4 5 6 7 8 (a null child still consumes a slot only if its…
- Non-negative numbers are stored as singly linked lists of digits with the least significant digit first. A = 9 -> 9 -> 8 -> 5 (the number…
- A priority queue is a sorted singly linked list (ascending). Insert scans from the head, comparing the new key with each node, and stops…
- Keys 1..7 form a max-heap stored in a 0-indexed array (children of i are 2i+1, 2i+2). The key 5 is the third largest. In how many…
- A hash table starts with 7 slots, uses linear probing with h(k) = k mod size, and after each insertion, if the load factor exceeds 0.7, it…