What if you could flip an entire tree upside down with just a few lines of code?
Why Mirror a Binary Tree in DSA Go?
Imagine you have a family tree drawn on paper. Now, you want to see how it looks if you flip it like a mirror image. Doing this by redrawing every branch and connection by hand is tiring and easy to mess up.
Manually flipping each branch means you must carefully swap every left and right child for all nodes. This is slow, confusing, and you might forget some branches or swap incorrectly, leading to mistakes.
Mirroring a binary tree with code swaps the left and right children of every node automatically. This simple process repeats for all nodes, giving you the mirror image quickly and without errors.
func mirrorTree(node *Node) {
if node == nil {
return
}
// Manually swap left and right for each node
temp := node.left
node.left = node.right
node.right = temp
mirrorTree(node.left)
mirrorTree(node.right)
}func mirrorTree(node *Node) {
if node == nil {
return
}
node.left, node.right = node.right, node.left
mirrorTree(node.left)
mirrorTree(node.right)
}This lets you quickly transform any tree structure into its mirror image, enabling new ways to analyze or visualize data.
In graphics or game development, mirroring a scene graph (a tree of objects) helps create reflections or symmetrical designs easily.
Manual flipping is slow and error-prone.
Code swaps children recursively for a perfect 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: 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 AQuick Check:
Mirror = Swap children [OK]
- Thinking mirror means deleting nodes
- Confusing mirror with sorting
- Assuming mirror means flattening the tree
Solution
Step 1: Understand pointer usage in Go trees
Binary trees are usually represented with pointers to nodes to allow modification.Step 2: Check return type for mirrored tree
The function should return a pointer to the root of the mirrored tree.Final Answer:
func mirrorTree(root *TreeNode) *TreeNode -> Option DQuick Check:
Pointer input and output for tree nodes [OK]
- Passing tree nodes by value instead of pointer
- Returning wrong type like int or bool
- Not returning the mirrored tree root
type TreeNode struct {
Val int
Left, Right *TreeNode
}
func mirrorTree(root *TreeNode) *TreeNode {
if root == nil {
return nil
}
left := mirrorTree(root.Left)
right := mirrorTree(root.Right)
root.Left, root.Right = right, left
return root
}
func main() {
root := &TreeNode{Val: 1}
root.Left = &TreeNode{Val: 2}
root.Right = &TreeNode{Val: 3}
mirrorTree(root)
fmt.Println(root.Val, root.Left.Val, root.Right.Val)
}Solution
Step 1: Understand the initial tree structure
Root is 1, left child is 2, right child is 3.Step 2: Apply mirrorTree function
Mirror swaps left and right children at root, so left becomes 3 and right becomes 2.Final Answer:
1 3 2 -> Option CQuick Check:
Swapped children values = 1 3 2 [OK]
- Not swapping children correctly
- Printing values before mirroring
- Confusing left and right child values
func mirrorTree(root *TreeNode) *TreeNode {
if root == nil {
return nil
}
mirrorTree(root.Left)
mirrorTree(root.Right)
root.Left, root.Right = root.Right, root.Left
return root
}Solution
Step 1: Analyze recursive calls
The function calls mirrorTree on root.Left and root.Right but ignores their returned mirrored subtrees.Step 2: Check assignment of mirrored subtrees
Without assigning returned values, the subtrees are not updated, so mirror is incomplete.Final Answer:
The recursive calls do not assign the mirrored subtrees back to root.Left and root.Right -> Option BQuick Check:
Assign returned mirrored subtrees to children [OK]
- Ignoring returned subtree pointers
- Swapping children before recursion
- Missing base case (though present here)
Solution
Step 1: Understand the goal
We want to count how many nodes had their children swapped during mirroring.Step 2: Modify mirror function to count swaps
Each time we swap left and right children at a node, increment a counter and add counts from recursive calls.Final Answer:
Add a counter variable and increment it each time left and right children are swapped recursively -> Option AQuick Check:
Count swaps during recursion [OK]
- Counting node values instead of swaps
- Not accumulating counts from recursive calls
- Using traversal without swapping
Ready to prove it?
Pass the test and this pattern moves unknown
