Data Structures & AlgorithmsHardProduct company OA · Infosys pattern
Keys 50, 40, 30, 45, 47 are inserted in that order into an empty AVL tree. What is the preorder traversal of the final tree?
- A. 45, 40, 30, 50, 47
- B. 40, 30, 45, 50, 47
- C. 47, 40, 30, 45, 50
- D. 40, 30, 47, 45, 50
Correct answer: D. 40, 30, 47, 45, 50
50, 40, 30 is an LL case → right rotation at 50 → root 40 with children 30 and 50. 45 goes to 50's left. 47 goes to 45's right, making 50 unbalanced (+2) with an LR shape (left child 45, then right 47): rotate left at 45, then right at 50 → 47 becomes the subtree root with 45 and 50. Preorder 40, 30, 47, 45, 50.
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
- Kruskal's algorithm runs on edges (weight): A–B 4, A–C 3, B–C 2, B–D 5, C–D 7, C–E 8, D–E 6, D–F 9, E–F 4. What is the total weight of the…
- Bellman–Ford from S relaxes the directed edges in this fixed order in every pass: C→D (1), B→C (2), A→B (3), S→A (4). How many passes are…
- A min-heap of size k = 3 tracks the 3 largest values of the stream 5, 1, 9, 3, 7, 8, 2 (push each value; if the size exceeds 3, pop the…
- An undirected graph has edges 1–2, 2–3, 3–1, 3–4, 4–5, 5–6, 6–4, 6–7, 7–8. How many BRIDGES (edges whose removal disconnects the graph)…
- Keys 20, 8, 22, 4, 12, 10, 14, 25 are inserted into an empty BST. What is the lowest common ancestor of 10 and 14, and of 4 and 14?
- Keys 50, 30, 70, 20, 40, 60, 80, 35, 45, 65 are inserted into an empty BST. A range query counts keys in [33, 62], pruning subtrees that…