Data Structures & AlgorithmsVery hardGATE CS
Keys 56, 16, 21, 93, 86, 85, 15 inserted in this order build a BST. How many of the 7! orderings of these same 7 keys, inserted into an empty BST, produce exactly the same tree?
- A. 40
- B. 20
- C. 2
- D. 80
Correct answer: A. 40
A node must come before every key in its subtree, but its two subtrees may interleave freely: count = C(n−1, |left|) × count(left) × count(right) = 40, which also equals n!/∏(subtree sizes).
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
- A tree on vertices 1–11 has edges 2–1, 3–2, 4–2, 5–3, 6–3, 7–4, 8–6, 9–7, 10–7, 11–8. What is the size of a maximum independent set (no…
- All suffixes of the string 'level' are inserted into a character trie (a suffix trie). How many nodes does it have, including the root?
- A DAG on vertices A–G has edges A→D, A→F, C→D, E→C, F→B, F→D, G→A, G→F. What order does Kahn's algorithm with a FIFO queue (initial zero…
- What is the edit distance (insert, delete, substitute; each cost 1) between DIAMETER and INTERVAL?
- Prim's algorithm starts at F on the weighted undirected graph A–B (18), A–F (15), B–E (26), B–F (23), B–G (21), C–D (9), C–E (16), D–E…
- A rod of length 8 can be cut into integer pieces; a piece of length i sells for p(i) with p(1..8) = 1, 2, 5, 14, 16, 22, 24, 25. What is…