Bird
Raised Fist0

Given the following code for inverting a binary tree, what is the value of the left child of the root node after calling invertTree(root) on the tree below? Tree structure before inversion:

easy🧾 Code Trace Q12 of Q15
Tree: Depth-First Search - Invert Binary Tree
Given the following code for inverting a binary tree, what is the value of the left child of the root node after calling invertTree(root) on the tree below? Tree structure before inversion:
    2
   / \
  1   3
A3
B1
C2
Dnull
Step-by-Step Solution
  1. Step 1: Trace recursive calls on root=2

    invertTree called on left child (1) and right child (3), both leaves, so their children are null and return immediately.
  2. Step 2: Swap left and right children of root=2

    After recursion, root.left and root.right are swapped: left becomes 3, right becomes 1.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Root's left child after inversion is original right child 3 [OK]
Quick Trick: Swapping children after recursion flips subtree positions [OK]
Common Mistakes:
MISTAKES
  • Confusing left and right child values after inversion
  • Forgetting to swap after recursive calls
  • Assuming root value changes
Trap Explanation:
PITFALL
  • Candidates often forget that swapping happens after recursion, so left child becomes original right child.
Interviewer Note:
CONTEXT
  • Checks ability to mentally execute recursion and swapping order.
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