📖 Scenario: You are working with a simple binary tree structure used in many applications like file systems and expression parsing. Sometimes, you need to create a mirror image of this tree, where the left and right children of all nodes are swapped.
🎯 Goal: Build a Java program that creates a binary tree, then mirrors it by swapping left and right children at every node, and finally prints the mirrored tree in order.
📋 What You'll Learn
Create a TreeNode class with int val, TreeNode left, and TreeNode right fields
Create a binary tree with the exact structure given
Write a method mirror(TreeNode root) that swaps left and right children recursively
Write an inOrder(TreeNode root) method to print the tree nodes in order
Print the in-order 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 manipulation is important for software engineers working with data structures, algorithms, and system design.
Progress0 / 4 steps
1
Create the TreeNode class and build the initial binary tree
Create a class called TreeNode with an integer field val, and two TreeNode fields called left and right. Then create a binary tree with root node value 1, left child value 2, and right child value 3. The left child of node 2 should be 4 and the right child should be 5. Assign this tree to a variable called root.
DSA Java
Hint
Define the TreeNode class first. Then create the root and assign its children exactly as described.
2
Add the mirror method to swap left and right children
Add a static method called mirror that takes a TreeNode root parameter. This method should swap the left and right children of the root node, then recursively call itself on the left and right children if they are not null.
DSA Java
Hint
Use a temporary variable to swap root.left and root.right. Then call mirror recursively on both children.
3
Add an inOrder method to print the tree nodes in order
Add a static method called inOrder that takes a TreeNode root parameter. This method should print the values of the tree nodes in in-order traversal (left, root, right) separated by spaces. Use recursion and print each value followed by a space without a newline.
DSA Java
Hint
Use recursion to visit left child, print current node value, then visit right child.
4
Call mirror and print the mirrored tree in order
In the main method, after building the tree, call mirror(root) to mirror the tree. Then call inOrder(root) to print the mirrored tree nodes in order on a single line.
DSA Java
Hint
Call mirror(root) first, then inOrder(root) to print the mirrored tree.
Practice
(1/5)
1. What does it mean to mirror a binary tree?
easy
A. Remove all leaf nodes from the tree
B. Swap the left and right children of every node in the tree
C. Convert the tree into a linked list
D. Sort the nodes in ascending order
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 B
Quick Check:
Mirror = Swap children [OK]
Hint: Mirror means swap left and right children at all nodes [OK]
Common Mistakes:
Thinking mirror means deleting nodes
Confusing mirror with sorting
Assuming mirror flattens the tree
2. Which of the following Java method signatures correctly represents a function to mirror a binary tree node?
easy
A. public void mirrorTree(Node root)
B. public int mirrorTree(Node root)
C. void mirrorTree(Node root) throws Exception
D. public Node mirrorTree()
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.
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=4
Step 3: Preorder traversal after mirror
Visit root, then left subtree, then right subtree:
1, 3, 5, 2, 4
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 C
Quick Check:
NullPointerException = missing null check [OK]
Hint: Always check if node is null before accessing children [OK]
Common Mistakes:
Ignoring null checks
Thinking swap order causes error
Changing return type unnecessarily
5. You have a binary tree where some nodes have only one child. After mirroring the tree using the standard mirror function, which of the following statements is true about the structure of the mirrored tree?
hard
A. Leaf nodes become internal nodes after mirroring
B. Nodes with one child will have two children after mirroring
C. The tree height will always increase by 1
D. Nodes with only left child will have only right child after mirroring
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 D
Quick Check:
Mirror swaps children, so left-only becomes right-only [OK]
Hint: Mirror swaps children, so single left child becomes single right child [OK]
Common Mistakes:
Assuming tree height changes
Thinking nodes gain extra children
Confusing leaf and internal nodes
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.