Bird
Raised Fist0

In an in-place flatten function for a binary tree, why might some nodes still have non-null left pointers after flattening?

medium🛠️🐞 Bug Fixing Q7 of Q15
Tree: Depth-First Search - Flatten Binary Tree to Linked List
In an in-place flatten function for a binary tree, why might some nodes still have non-null left pointers after flattening?
AThe tree is not a binary tree
BThe recursion does not visit all nodes
CThe algorithm fails to explicitly set left pointers to null after rewiring
DThe right pointers are not updated correctly
Step-by-Step Solution
Solution:
  1. Step 1: Understand flatten requirements

    All left pointers must be set to null in the flattened list.
  2. Step 2: Identify common bug

    Some implementations forget to nullify left pointers after rewiring right pointers.
  3. Step 3: Consequence

    Left pointers remain non-null, violating the singly linked list structure.
  4. Final Answer:

    Option C -> Option C
  5. Quick Check:

    Always nullify left pointers after rewiring [OK]
Quick Trick: Always set left pointers to null after rewiring [OK]
Common Mistakes:
MISTAKES
  • Assuming rewiring right pointers is sufficient
  • Missing base case in recursion
  • Confusing left and right pointer updates
Trap Explanation:
PITFALL
  • Forgetting to nullify left pointers looks like partial flattening
Interviewer Note:
CONTEXT
  • Tests attention to detail in pointer manipulation
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