Bird
Raised Fist0

Consider the optimal flatten function using reverse preorder traversal with a global pointer. What is the output linked list when the input tree is a single node with value 42?

medium🧾 Code Trace Q4 of Q15
Tree: Depth-First Search - Flatten Binary Tree to Linked List
Consider the optimal flatten function using reverse preorder traversal with a global pointer. What is the output linked list when the input tree is a single node with value 42?
A[42, None]
B[None]
C[]
D[42]
Step-by-Step Solution
Solution:
  1. Step 1: Trace flatten on single node

    Recursion calls flatten on None children, returns immediately.
  2. Step 2: Pointer rewiring

    Node's right pointer set to prev (None), left pointer set to None, prev updated to node.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Single node remains as is in linked list [OK]
Quick Trick: Single node flatten returns node itself [OK]
Common Mistakes:
MISTAKES
  • Expecting extra None in output
  • Confusing empty list with single node
Trap Explanation:
PITFALL
  • Candidates sometimes think output includes None explicitly or empty list for single node.
Interviewer Note:
CONTEXT
  • Tests handling of minimal edge case input.
Master "Flatten Binary Tree to Linked List" 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