Data Structures & AlgorithmsVery hardGATE CS · Codeforces Div 2
Keys are inserted in the order 11 88 20 15 45 into an empty binary search tree. Then the key 20 (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. 11 88 15 45
- B. 11 88 20 15 45
- C. 11 88 45 15 45
- D. 11 88 45 15
Correct answer: D. 11 88 45 15
The successor (smallest key of the right subtree) replaces 20, so the preorder becomes 11 88 45 15. 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
- Consider the recursive function def f(n): if n < 2: return n; return f(n-1) + f(n-2). Evaluate f(6) and count every invocation of f…
- Words ['bdc', 'dd', 'dba', 'ddc', 'ccb'] (each may be used at most once). A chain is a sequence of these words in which the first letter…
- Array A = [2, 3, 3, 4, 2, 2, 3, 5, 2]. An increasing subsequence is chosen by INDEX positions i1 < i2 < … with A[i1] < A[i2] < … (strictly…
- A trie keeps a counter at each node, and every word of the list W = ['ab', 'cc', 'ba', 'ca', 'ac', 'ba', 'cba', 'bba'] is inserted once…
- Connected undirected graph on vertices A, B, C, D, E, F, G with edges A–D (1), A–E (1), A–F (1), B–E (1), B–F (1), C–D (1), D–F (1), D–G…
- Connected undirected weighted graph on vertices A, B, C, D, E, F with edges A–B (1), A–C (14), A–F (12), B–D (12), C–D (8), C–E (1), E–F…