Data Structures & AlgorithmsVery hardProduct company OA
Keys 1 to 7 are inserted in increasing order into an empty AVL tree, then 4 is deleted (a node with two children is replaced by its inorder successor) and then 5 is deleted. What is the preorder traversal of the final tree?
- A. 6, 2, 1, 3, 5, 7
- B. 6, 2, 1, 3, 7
- C. 3, 2, 1, 6, 7
- D. 2, 1, 6, 3, 7
Correct answer: B. 6, 2, 1, 3, 7
Inserting 1–7 in order gives the perfect tree 4 (2 (1, 3), 6 (5, 7)). Deleting 4 replaces it with its successor 5, leaving 5 (2 (1, 3), 6 (–, 7)). Deleting the root 5 now uses successor 6: 6 (2 (1, 3), 7). Heights are 2 and 1, still balanced, so no rotation: preorder 6, 2, 1, 3, 7.
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
- What is the size of a minimum vertex cover (a smallest set of vertices touching every edge) of the tree with edges 1–2, 1–3, 2–4, 2–5…
- In the tree with edges 1–2, 1–3, 2–4, 2–5, 3–6, 5–7, the rerooting technique computes S(v) = sum of distances from v to every other node…
- Each node of this binary tree holds a value; how many DOWNWARD paths (starting at any node, moving only to children, ending at any node)…
- Weighted undirected graph: A–B 1, B–C 2, C–D 3, D–A 4, A–C 5, B–D 6. The MST is unique. What is the weight of the SECOND-best spanning…
- Word ladder: change one letter at a time, every intermediate word must be in the dictionary {cold, cord, card, ward, warm, word, worm…
- In this grid, how many islands of 1s are there with 4-directional connectivity, and how many with 8-directional (diagonals also connect)?…