What if you could flip a whole tree with just a few lines of code, no mistakes?
Why Mirror a Binary Tree in DSA C?
Imagine you have a family tree drawn on paper, showing parents and children on the left and right sides. Now, you want to flip the whole tree so that left children become right children and vice versa, but doing this by hand means redrawing every connection carefully.
Manually redrawing or swapping each branch is slow and easy to mess up. You might forget to swap some parts or make mistakes in the connections, leading to a wrong or broken tree. It's tiring and error-prone, especially for big trees.
Mirroring a binary tree with code swaps the left and right children of every node automatically. This simple, clear process quickly flips the entire tree without missing any part, saving time and avoiding mistakes.
void mirrorTree(Node* root) {
// Manually swap left and right pointers for each node
// But no clear process, easy to forget nodes
}void mirrorTree(Node* root) {
if (root == NULL) return;
Node* temp = root->left;
root->left = root->right;
root->right = temp;
mirrorTree(root->left);
mirrorTree(root->right);
}This lets you quickly flip any binary tree structure, enabling easy transformations and new views of hierarchical data.
In graphics or UI, mirroring a tree structure can help flip layouts or reverse decision paths, like flipping a family tree or game decision tree to see the opposite side.
Manual flipping is slow and error-prone.
Code swaps children recursively for a full mirror.
Enables quick, reliable tree transformations.
Practice
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 BQuick Check:
Mirror = Swap children [OK]
- Thinking mirror means sorting nodes
- Confusing mirror with deleting nodes
- Assuming mirror counts nodes
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.Final Answer:
void mirrorTree(struct Node* root); -> Option AQuick Check:
Pointer parameter + void return = void mirrorTree(struct Node* root); [OK]
- Passing struct Node by value instead of pointer
- Returning struct Node instead of void
- Using int return type incorrectly
struct Node {
int data;
struct Node* left;
struct Node* right;
};
void mirrorTree(struct Node* root) {
if (root == NULL) return;
struct Node* temp = root->left;
root->left = root->right;
root->right = temp;
mirrorTree(root->left);
mirrorTree(root->right);
}
// Tree before mirror:
// 1
// / \
// 2 3
// / \
// 4 5
// After mirror, print preorder traversal:
// Expected output?
Solution
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=NULLStep 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 4Final Answer:
1 3 5 2 4 -> Option DQuick Check:
Mirror + preorder = 1 3 5 2 4 [OK]
- Not swapping children correctly
- Confusing preorder with inorder or postorder
- Missing leaf nodes in traversal
void mirrorTree(struct Node* root) {
if (root == NULL) return;
mirrorTree(root->left);
struct Node* temp = root->left;
root->left = root->right;
root->right = temp;
mirrorTree(root->right);
}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 CQuick Check:
Swap first, recurse later [OK]
- Swapping after recursion causes incorrect mirror
- Removing base case causes crashes
- Changing temp type breaks pointer logic
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 AQuick Check:
Mirror flips positions, leaf count unchanged [OK]
- Thinking leaf nodes become internal nodes
- Assuming nodes are added or removed
- Believing leaf count changes
Ready to prove it?
Pass the test and this pattern moves unknown
