📖 Scenario: Imagine you have a family tree represented as a binary tree. You want to create a mirror image of this tree, swapping left and right children at every node.
🎯 Goal: Build a program in C that creates a binary tree, then mirrors it by swapping left and right children recursively, and finally prints the mirrored tree in inorder traversal.
📋 What You'll Learn
Define a binary tree node structure with integer data and left/right pointers
Create a binary tree with exactly 3 nodes: root with value 1, left child 2, right child 3
Create a function to mirror the binary tree by swapping left and right children recursively
Print the inorder traversal of the mirrored tree
💡 Why This Matters
🌍 Real World
Mirroring trees is useful in graphics, image processing, and reversing hierarchical data structures.
💼 Career
Understanding tree manipulations is important for software engineers working with data structures, algorithms, and system design.
Progress0 / 4 steps
1
Create the binary tree structure and initial tree
Define a struct called Node with an int data and two pointers left and right to Node. Then create three nodes: root with data 1, root->left with data 2, and root->right with data 3.
DSA C
Hint
Use typedef struct Node to define the node. Use malloc to create nodes and assign data values 1, 2, and 3.
2
Create a function to mirror the binary tree
Write a function called mirror that takes a pointer to Node called root. If root is not NULL, recursively call mirror on root->left and root->right, then swap root->left and root->right pointers.
DSA C
Hint
Use recursion to call mirror on left and right children, then swap the pointers using a temporary variable.
3
Call the mirror function on the tree
In main, call the function mirror with the argument root to mirror the binary tree.
DSA C
Hint
Simply call mirror(root); inside main before returning.
4
Print the inorder traversal of the mirrored tree
Write a function called inorder that takes a pointer to Node called root. It should print the data of nodes in inorder (left, root, right) traversal separated by spaces. Then call inorder(root) in main after mirroring the tree.
DSA C
Hint
Use recursion to print left subtree, then root data, then right subtree. Call inorder(root); after mirroring and print a newline.
Practice
(1/5)
1. What does mirroring a binary tree mean?
easy
A. Sorting the nodes in ascending order
B. Swapping the left and right children of every node in the tree
C. Removing all leaf nodes from the tree
D. Counting the total number of nodes in the tree
Solution
Step 1: Understand the mirror operation
Mirroring a binary tree means swapping the left and right children of every node.
Step 2: Identify the effect on the tree
This operation flips the tree around its center, creating a mirror image.
Final Answer:
Swapping the left and right children of every node in the tree -> Option B
Quick Check:
Mirror = Swap children [OK]
Hint: Mirror means swap left and right children at all nodes [OK]
Common Mistakes:
Thinking mirror means sorting nodes
Confusing mirror with deleting nodes
Assuming mirror counts nodes
2. Which of the following is the correct C function signature to mirror a binary tree node?
easy
A. void mirrorTree(struct Node* root);
B. int mirrorTree(struct Node root);
C. struct Node mirrorTree(struct Node* root);
D. void mirrorTree(struct Node root);
Solution
Step 1: Understand parameter passing for trees in C
Binary trees are usually passed as pointers to nodes to allow modification.
Step 2: Identify correct return type
Mirroring modifies the tree in place, so the function returns void.
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