Step 1: Understand the mirror operation on the tree
Swapping left and right children at every node flips the tree:
Original root 1 has left=2, right=3
After mirror: left=3, right=2
Node 2 has left=4, right=NULL
After mirror: left=NULL, right=4
Node 3 has left=NULL, right=5
After mirror: left=5, right=NULL
Step 2: Perform preorder traversal on mirrored tree
Preorder: root, left, right
Visit 1
Go left to 3
Go left to 5 (leaf)
Back to 1, go right to 2
Go left NULL, go right 4
Sequence: 1 3 5 2 4
C. Swap children before recursive calls to mirrorTree
D. Change temp to int instead of struct Node*
Solution
Step 1: Analyze the order of operations in the function
The function calls mirrorTree on left and right children before swapping them.
This causes the recursive calls to work on original children, not swapped ones.
Step 2: Correct the order to swap first, then recurse
Swapping children first ensures recursive calls mirror the swapped subtrees correctly.
Final Answer:
Swap children before recursive calls to mirrorTree -> Option C
Quick Check:
Swap first, recurse later [OK]
Hint: Swap children before recursion to mirror correctly [OK]
Common Mistakes:
Swapping after recursion causes incorrect mirror
Removing base case causes crashes
Changing temp type breaks pointer logic
5. Given a binary tree where some nodes have only one child, what is the effect of mirroring the tree on the structure and leaf nodes?
Choose the correct statement.
hard
A. All leaf nodes remain leaf nodes but their positions are mirrored
B. Some leaf nodes become internal nodes after mirroring
C. The number of leaf nodes doubles after mirroring
D. Mirroring removes all nodes with only one child
Solution
Step 1: Understand mirroring effect on node positions
Mirroring swaps left and right children but does not add or remove nodes.
Leaf nodes remain leaf nodes but their positions flip left to right.
Step 2: Confirm leaf nodes count and structure
Number of leaf nodes stays the same; no nodes become internal or are removed.
Only their left-right positions change.
Final Answer:
All leaf nodes remain leaf nodes but their positions are mirrored -> Option A