Bird
Raised Fist0

Given the following partial traversal output of a composite tree iterator: ['root', 'child1', 'leafA', 'leafB', 'child2', 'leafC'], which of the following original tree structures is consistent with this output?

hard🔄 Reverse Engineer Q9 of Q15
OOP & Design Patterns - Iterator & Composite Pattern - Traversal Abstractions
Given the following partial traversal output of a composite tree iterator: ['root', 'child1', 'leafA', 'leafB', 'child2', 'leafC'], which of the following original tree structures is consistent with this output?
Aroot with children: child1 (with leafA, leafB), child2 (with leafC)
Broot with children: child2 (with leafC), child1 (with leafA, leafB)
Croot with children: leafA, child1 (with leafB), child2 (with leafC)
Droot with children: child1 (with leafA), leafB, child2 (with leafC)
Step-by-Step Solution
Solution:
  1. Step 1: Analyze traversal order

    Output shows 'child1' before 'leafB', indicating leafB is child of child1, not sibling.
  2. Step 2: Match tree structure to output

    Only root with children: child1 (with leafA, leafB), child2 (with leafC) places leafB as child of child1, consistent with output order.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Traversal order matches root with children: child1 (with leafA, leafB), child2 (with leafC) structure [OK]
Quick Trick: Traversal order reveals parent-child relationships [OK]
Common Mistakes:
MISTAKES
  • Assuming all leaves under first child
  • Ignoring sibling order
  • Misreading traversal sequence
Trap Explanation:
PITFALL
  • Candidates often misinterpret traversal order and parent-child relations.
Interviewer Note:
CONTEXT
  • Tests deep understanding of traversal output and tree structure correlation.
Master "Iterator & Composite Pattern - Traversal Abstractions" in OOP & Design Patterns

2 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 OOP & Design Patterns Quizzes