Data Structures & AlgorithmsVery hardCodeforces Div 2 · ICPC regional
Keys are inserted in the order 51 18 15 34 11 into an empty binary search tree. Then the key 18 (which has two children) is deleted by replacing it with its INORDER SUCCESSOR, and the successor node is removed from the right subtree. What is the preorder traversal (root, left, right) of the tree afterwards? Give the keys separated by spaces.
- A. 51 18 15 11 34
- B. 51 15 11 34
- C. 51 34 15 11 34
- D. 51 34 15 11
Correct answer: D. 51 34 15 11
The successor (smallest key of the right subtree) replaces 18, so the preorder becomes 51 34 15 11. Using the predecessor instead gives a different shape, and forgetting to remove the successor node leaves a duplicate.
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
- Connected undirected graph on vertices A, B, C, D, E, F, G, H with edges A–C (1), B–D (1), B–E (1), B–G (1), C–D (1), D–F (1), D–G (1)…
- Undirected weighted graph on vertices A, B, C, D, E, F; its edges, in this listed order, are: A–C (7), B–D (9), A–D (6), A–E (5), B–F (6)…
- Points A(5,6), B(6,6), C(4,5), D(7,8), E(7,6) in the plane. The cost of joining two points is the Manhattan distance |x1−x2| + |y1−y2|…
- Dictionary D = {bdc, bds, cat, gce, lpb, nig, pgm, rmm, seg, tgm, tgs, tms} (all words have 3 letters). Starting from the word 'pmm', a…
- Undirected graph on vertices A, B, C, D, E, F with edges, in this exact order: C–D (1), B–C (1), C–E (1), C–F (1), A–D (1), A–C (1)…
- A chain of 5 matrices M1 to M5 has dimensions given by the sequence p = [12, 16, 8, 10, 7, 10], where Mi has size p[i−1] × p[i]…