Bird
Raised Fist0

Given the following optimized recursive code snippet for building a tree from inorder and postorder traversals, what is the value of the root node constructed for inorder = [9,3,15,20,7] and postorder = [9,15,7,20,3]?

easy🧾 Code Trace Q3 of Q15
Tree: Depth-First Search - Construct Tree from Inorder and Postorder
Given the following optimized recursive code snippet for building a tree from inorder and postorder traversals, what is the value of the root node constructed for inorder = [9,3,15,20,7] and postorder = [9,15,7,20,3]?
A20
B15
C9
D3
Step-by-Step Solution
Solution:
  1. Step 1: Identify root from postorder last element

    postorder[-1] = 3, so root node value is 3.
  2. Step 2: Confirm root value matches expected tree root

    3 is the root splitting inorder into left [9] and right [15,20,7].
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Root is last postorder element 3 [OK]
Quick Trick: Root is last element in postorder traversal [OK]
Common Mistakes:
MISTAKES
  • Picking first element of inorder or postorder
  • Mixing up traversal roles
Trap Explanation:
PITFALL
  • Candidates often confuse root position in traversals, picking wrong root value.
Interviewer Note:
CONTEXT
  • Tests ability to trace recursive tree construction on canonical input.
Master "Construct Tree from Inorder and Postorder" in Tree: Depth-First Search

3 interactive learning modes - each teaches the same concept differently

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Tree: Depth-First Search Quizzes