Data Structures & AlgorithmsVery hardCodeforces Div 2 · ICPC regional
Keys are inserted in the order 40 89 17 61 57 into an empty binary search tree. Then the key 40 (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. 57 17 89 61
- B. 57 17 89 61 57
- C. 40 17 89 61 57
- D. 17 89 61 57
Correct answer: A. 57 17 89 61
The successor (smallest key of the right subtree) replaces 40, so the preorder becomes 57 17 89 61. 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
- Array A = [11, 11, 2, 3, 14, 14]. Over all non-empty contiguous subarrays, what is the maximum value of the XOR of the elements in the…
- Undirected simple graph on vertices A, B, C, D, E, F, G with edges A–C (1), A–D (1), A–F (1), B–C (1), B–D (1), B–F (1), C–F (1), D–F (1)…
- Undirected simple graph on vertices A, B, C, D, E, F, G with edges A–D (1), B–C (1), B–E (1), B–F (1), B–G (1), C–D (1), C–F (1), C–G (1)…
- Undirected weighted graph on vertices A, B, C, D, E, F with edges A–B (4), A–C (5), A–D (6), C–D (5), C–E (7), D–E (5), D–F (5), E–F (6)…
- Array [13, 15, 17, 14, 6, 23, 3] is turned into a min-heap in place with bottom-up heapify: for i from ⌊n/2⌋−1 down to 0, sift the element…
- Keys [12, 24, 3, 18, 30, 33, 7, 16, 4] are inserted, in this order, into an empty AVL tree using the standard rebalancing (left, right…