Data Structures & AlgorithmsVery hardCodeforces Div 2 · ICPC regional
Keys are inserted in the order 12 48 72 44 38 26 into an empty binary search tree. Then the key 48 (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. 12 72 44 38 26
- B. 12 48 44 38 26 72
- C. 12 72 44 38 26 72
- D. 12 44 38 26 72
Correct answer: A. 12 72 44 38 26
The successor (smallest key of the right subtree) replaces 48, so the preorder becomes 12 72 44 38 26. 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
- Words ['bcb', 'cacb', 'bbba', 'bcb', 'cbaa', 'b', 'abcc'] are inserted in this order into an empty trie with one node per character…
- Complete undirected graph on vertices A to F with symmetric edge weights given row by row (row i, column j = weight of edge between…
- A binary trie of 6-bit numbers stores the set S = [45, 4, 46, 21]. For the query x = 48, what is the maximum value of x XOR s over all s…
- Undirected simple graph on vertices A, B, C, D, E, F, G with edges A–B (1), A–C (1), A–G (1), B–G (1), C–D (1), C–F (1), C–G (1), D–E (1)…
- A binary trie of 6-bit numbers stores the set S = [23, 17, 24, 54, 21, 27, 41]. For the query x = 40, what is the maximum value of x XOR s…
- 2 × 3 sliding puzzle with tiles 1 to 5 and one blank (shown as ). Current board: 4 2 / 5 1 3. Goal board: 1 2 3 / 4 5 . A move slides a…