📖 Scenario: You are working on a simple program that deals with binary trees. A binary tree is like a family tree where each person has up to two children: a left child and a right child.Sometimes, you want to flip this tree so that the left and right children of every node swap places. This is called mirroring the tree.
🎯 Goal: Build a Go 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 a simple format.
📋 What You'll Learn
Create a binary tree node struct with integer values and left/right pointers
Build a specific binary tree with given values
Write a function to mirror the binary tree by swapping left and right children recursively
Print the tree nodes in preorder traversal after mirroring
💡 Why This Matters
🌍 Real World
Mirroring trees is useful in graphics, image processing, and data structure transformations.
💼 Career
Understanding tree manipulations and recursion is essential for software engineering roles involving data structures and algorithms.
Progress0 / 4 steps
1
Create the binary tree structure and build the tree
Define a struct called Node with fields Val int, Left *Node, and Right *Node. Then create a variable root that builds this exact tree:
1 / \\ 2 3 / / \\ 4 5 6
Use &Node{Val: value, Left: ..., Right: ...} syntax to build the tree.
DSA Go
Hint
Start by defining the Node struct with the three fields. Then build the tree from the bottom up using nested &Node{} expressions.
2
Add a helper function to mirror the tree
Create a function called mirror that takes a pointer to Node named root. This function should swap the Left and Right children of root and then call itself recursively on both children if they are not nil.
DSA Go
Hint
Check if root is nil. If not, swap Left and Right. Then call mirror recursively on both children.
3
Call the mirror function on the tree
In the main function, call the mirror function with the root node to mirror the entire tree.
DSA Go
Hint
Simply call mirror(root) inside main after building the tree.
4
Print the mirrored tree in preorder traversal
Create a function called printPreorder that takes a pointer to Node named root. It should print the Val of each node in preorder (root, left, right) separated by spaces. Then call printPreorder(root) in main after mirroring the tree.
DSA Go
Hint
Print the current node's value, then recursively print the left subtree, then the right subtree. Call this function after mirroring.
Practice
(1/5)
1. What does it mean to mirror a binary tree?
easy
A. Swap the left and right children of every node in the tree
B. Remove all leaf nodes from 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 A
Quick Check:
Mirror = Swap children [OK]
Hint: Mirror means swapping left and right children at all nodes [OK]
Common Mistakes:
Thinking mirror means deleting nodes
Confusing mirror with sorting
Assuming mirror means flattening the tree
2. Which Go function signature correctly represents a function to mirror a binary tree node?
easy
A. func mirrorTree(root *TreeNode) int
B. func mirrorTree(root TreeNode) TreeNode
C. func mirrorTree(root TreeNode) bool
D. func mirrorTree(root *TreeNode) *TreeNode
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 D
Quick Check:
Pointer input and output for tree nodes [OK]
Hint: Use pointers for tree nodes to modify and return [OK]
Common Mistakes:
Passing tree nodes by value instead of pointer
Returning wrong type like int or bool
Not returning the mirrored tree root
3. What is the output of the following Go code that mirrors a binary tree?
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)
}
medium
A. 2 1 3
B. 1 2 3
C. 1 3 2
D. 3 1 2
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 C
Quick Check:
Swapped children values = 1 3 2 [OK]
Hint: Mirror swaps left and right children recursively [OK]
Common Mistakes:
Not swapping children correctly
Printing values before mirroring
Confusing left and right child values
4. Identify the error in this Go function that attempts to mirror a binary tree: