What if you could flip an entire family tree in seconds without redrawing a single line?
Why Mirror a Binary Tree in DSA Java?
Imagine you have a family tree drawn on paper, showing parents and children on the left and right sides. Now, you want to flip this tree so that everything on the left moves to the right and vice versa, like looking at it in a mirror.
Doing this flip by hand means you must carefully redraw every branch and connection. It is slow, easy to make mistakes, and hard to keep track of all the relationships correctly.
Using a computer program to mirror the tree swaps the left and right children of every node automatically. This saves time, avoids errors, and works perfectly even for very big trees.
void mirrorTree(Node node) {
// Manually swap left and right children for each node
// Repeat for all nodes
}void mirrorTree(Node node) {
if (node == null) return;
Node temp = node.left;
node.left = node.right;
node.right = temp;
mirrorTree(node.left);
mirrorTree(node.right);
}This lets you quickly transform any tree structure into its mirror image, enabling new ways to analyze or display data.
In graphics or games, mirroring a tree structure can flip scenes or objects horizontally, creating reflections or symmetrical designs easily.
Manually flipping a tree is slow and error-prone.
Mirroring swaps left and right children recursively.
Automated mirroring works fast even on large trees.
Practice
Solution
Step 1: Understand the mirror operation
Mirroring a binary tree means swapping the left and right children of every node.Step 2: Apply the definition to the tree
By swapping children at every node, the tree becomes a mirror image of the original.Final Answer:
Swap the left and right children of every node in the tree -> Option BQuick Check:
Mirror = Swap children [OK]
- Thinking mirror means deleting nodes
- Confusing mirror with sorting
- Assuming mirror flattens the tree
Solution
Step 1: Identify correct return type
Mirroring modifies the tree in place, so void return type is appropriate.Step 2: Check method parameters
The method should accept the root node to start mirroring from.Final Answer:
public void mirrorTree(Node root) -> Option AQuick Check:
Void return + root parameter = correct signature [OK]
- Using return type int instead of void
- Missing root parameter
- Adding unnecessary exceptions
1
/ \
2 3
/ \
4 5
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);
}
Solution
Step 1: Understand the mirror function
The function swaps left and right children at every node recursively.Step 2: Apply mirror to the tree
Original tree preorder: 1,2,4,3,5. After mirror, left and right children swap: - Root 1: left=3, right=2 - Node 3: left=5, right=null - Node 2: left=null, right=4Step 3: Preorder traversal after mirror
Visit root, then left subtree, then right subtree: 1, 3, 5, 2, 4Final Answer:
[1, 3, 5, 2, 4] -> Option AQuick Check:
Mirror swaps children; preorder = [1,3,5,2,4] [OK]
- Not swapping children correctly
- Confusing preorder with inorder
- Missing recursive calls
public void mirrorTree(Node root) {
Node temp = root.left;
root.left = root.right;
root.right = temp;
mirrorTree(root.left);
mirrorTree(root.right);
}Solution
Step 1: Identify cause of NullPointerException
The code accesses root.left without checking if root is null.Step 2: Fix by adding null check
Adding "if (root == null) return;" at the start prevents errors when root is null.Final Answer:
Missing null check before accessing root.left -> Option CQuick Check:
NullPointerException = missing null check [OK]
- Ignoring null checks
- Thinking swap order causes error
- Changing return type unnecessarily
Solution
Step 1: Understand mirroring effect on children
Mirroring swaps left and right children at every node.Step 2: Analyze nodes with one child
If a node has only a left child, after mirroring it will have only a right child (the left child moves to right).Step 3: Check other options
Tree height does not necessarily increase; leaf nodes remain leaves; nodes do not gain extra children.Final Answer:
Nodes with only left child will have only right child after mirroring -> Option DQuick Check:
Mirror swaps children, so left-only becomes right-only [OK]
- Assuming tree height changes
- Thinking nodes gain extra children
- Confusing leaf and internal nodes
Ready to prove it?
Pass the test and this pattern moves unknown
