GATE Computer ScienceVery hardGATE CS (PYQ pattern)
Keys 1 to 5 are inserted into an empty BST in a uniformly random order of the 120 possible orders. In how many of the orders does key 3 end up at depth 1 (a child of the root)?
- A. 28
- B. 24
- C. 48
- D. 40
Correct answer: A. 28
Building the BST for each of the 120 orders and recording the depth of key 3 gives depth 0: 24, depth 1: 28, depth 2: 38, depth 3: 24, depth 4: 6 orders.
Practise more Programming and Data Structures (GATE) questionsProgramming and Data Structures (GATE): notes and key points
More Programming and Data Structures (GATE) questions
- Iterative postorder uses two stacks: push the root on S1; repeat: pop x from S1, push x on S2, push x's left child then its right child on…
- struct N { int v; int next; }; struct N a[6] = {{10,3},{20,5},{30,-1},{40,1},{50,2},{60,4}}; int i = 0, s = 0, k = 1; while (i != -1) { s…
- How many structurally distinct binary search trees on keys 1..7 have key 4 at the root and height exactly 3 (a single node has height 0)?
- char s = "abracadabra"; int c[26] = {0}, i; for (i = 0; s[i]; i++) c[s[i] - 'a']++; int best = 0; for (i = 1; i < 26; i++) if (c[i] (i +…
- For n = 7 distinct keys, what is the maximum number of swaps that the bottom-up (Floyd) build-heap for a max-heap can perform, over all…
- The stock span of day i is the number of consecutive days ending at i (including i) whose price is at most the price on day i. Prices are…