Data Structures & AlgorithmsHardProduct-company MCQ pattern
A binary tree has inorder 9 5 1 7 2 12 8 4 3 11 and postorder 9 1 2 12 7 5 3 11 4 8. What is its preorder?
- A. 8 9 5 7 1 12 2 4 11 3
- B. 8 5 9 7 1 2 12 4 11 3
- C. 8 5 7 9 1 12 2 4 3 11
- D. 8 5 9 7 1 12 2 4 11 3
Correct answer: D. 8 5 9 7 1 12 2 4 11 3
The root is the last postorder key, 8; it splits the inorder into 9 5 1 7 2 12 and 4 3 11. On the left the root is 5 (with 9 on its left) and 7 has children 1 and 12, where 12 has the left child 2. On the right 4 has right child 11 whose left child is 3. Preorder: 8 5 9 7 1 12 2 4 11 3.
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
- Keys 1, 2, 3, 4, 5, 6, 7 are inserted in that order into an empty AVL tree. How many rotations (counting a double rotation as one…
- What is the maximum possible height of an AVL tree with 20 nodes (a single node has height 0)?
- The words car, cart, care, cat, dog are inserted into an empty trie (one node per character, shared prefixes share nodes). How many nodes…
- How many calls in total (including the first) does C(5, 2) make? c int C(int n, int k) { if (k == 0 || k == n) return 1; return C(n - 1, k…
- For n = 6, how many disk moves does this Tower of Hanoi function count, and how many calls of hanoi are made in total? c int hanoi(int n…
- A robot moves only right or down on a 3 × 4 grid of cells (3 rows, 4 columns) from the top-left cell to the bottom-right cell. The cell in…