Data Structures & AlgorithmsModerateTCS NQT · Infosys pattern
Keys 40, 20, 60, 10, 30, 50, 70, 25 are inserted into an empty BST. Then 20 is deleted (a node with two children is replaced by its inorder successor). What is the preorder traversal?
- A. 40, 30, 10, 25, 60, 50, 70
- B. 40, 10, 30, 25, 60, 50, 70
- C. 40, 25, 30, 10, 60, 50, 70
- D. 40, 25, 10, 30, 60, 50, 70
Correct answer: D. 40, 25, 10, 30, 60, 50, 70
20 has two children, so it is replaced by its inorder successor – the smallest key in its right subtree (30 → its left child 25). 25 moves up and its old position becomes empty. Preorder: 40, 25, 10, 30, 60, 50, 70. Using 30 (the right child) instead of the true successor gives 40, 30, 10, 25, 60, 50, 70.
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 3-ary (ternary) heap is stored in a 0-indexed array where node i has children 3i + 1, 3i + 2 and 3i + 3. What is the index of the parent…
- An undirected graph has edges A–B, A–C, B–D, C–D, C–E, D–F, E–G, F–G, G–H. How many vertices are at a shortest distance of EXACTLY 2 edges…
- A graph has 9 vertices and 3 connected components. How many edges does a minimum spanning FOREST (an MST of every component) contain?
- Kahn's algorithm uses a MIN-heap (smallest available vertex first) on the DAG with edges 3→1, 3→4, 2→4, 4→0, 1→0, 5→2. What topological…
- The words bat, bath, bad, ban, band are inserted into an empty trie (one node per character, plus the root). How many nodes does the trie…
- Keys 50, 30, 70, 20, 40, 60, 80, 35, 65, 10 are inserted into an empty BST. How many nodes have EXACTLY one child?