Bird
Raised Fist0

Consider the invertTree function below. What will be the output when called with a single-node tree (root value 5, no children)?

medium🧾 Code Trace Q4 of Q15
Tree: Depth-First Search - Invert Binary Tree
Consider the invertTree function below. What will be the output when called with a single-node tree (root value 5, no children)?
ANone
BTree with root 5 and left child None, right child None
CTree with root 5 and left child 5, right child None
DTree with root 5 and left child None, right child 5
Step-by-Step Solution
Solution:
  1. Step 1: Trace recursion on single node

    Root has no children, so recursive calls on left and right return immediately.
  2. Step 2: Swap children

    Swapping None and None leaves children unchanged; root remains with no children.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    Single node tree remains unchanged after inversion [OK]
Quick Trick: Single node inversion -> no change [OK]
Common Mistakes:
MISTAKES
  • Assuming children become root or duplicate nodes
  • Confusing None with node values
Trap Explanation:
PITFALL
  • Candidates sometimes think inversion adds or duplicates nodes in single-node trees.
Interviewer Note:
CONTEXT
  • Tests handling of edge cases in recursion
Master "Invert Binary Tree" 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