Data Structures & AlgorithmsVery hardICPC regional · GATE CS top band
Distinct keys [5, 4, 7, 27, 25] are inserted, in the order given by the sequence [25, 5, 4, 27, 7], into an empty binary search tree. How many of the permutations of these same keys, when inserted in that order into an empty BST, produce exactly the same tree (same keys in the same positions)?
- A. 42
- B. 120
- C. 8
- D. 24
Correct answer: C. 8
Every order giving the same tree must start with the root and follow the same nesting, and there are exactly 8 of them. The total n! or the count matching only the shape are different quantities.
Practise more Trees, Graphs, Recursion and Dynamic Programming questionsTrees, Graphs, Recursion and Dynamic Programming: notes and key points
More Trees, Graphs, Recursion and Dynamic Programming questions
- Consider the recursive function def f(n): if n < 2: return n; return f(n-1) + f(n-2). Evaluate f(7) and count every invocation of f…
- Consider the recursive function def f(n): if n < 2: return n; return f(n-1) + f(n-2). Evaluate f(10) and count every invocation of f…
- Array [25, 3, 24, 17, 29, 1] is turned into a min-heap in place with bottom-up heapify: for i from ⌊n/2⌋−1 down to 0, sift the element at…
- Array A = [8, 27, 25, 23, 28, 20, 13] (all values below 32). What is the maximum of A[i] XOR A[j] over all index pairs with 0 ≤ i < j < 7?
- Undirected weighted graph on vertices A, B, C, D, E; its edges, in this listed order, are: C–E (9), A–C (8), A–B (5), B–E (3), D–E (2)…
- Union–find on elements 0 to 7, all starting as singletons, with no path compression. union(x, y): let rx, ry be the roots of x and y; if…