Mirroring a binary tree means swapping the left and right children of every node in the tree, creating a mirror image of the original tree.
Click to reveal answer
beginner
In Go, how do you define a simple binary tree node struct?
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
Click to reveal answer
beginner
What is the base case when recursively mirroring a binary tree?
The base case is when the current node is nil (empty), meaning there is no node to mirror, so the function returns immediately.
Click to reveal answer
intermediate
Why do we swap the left and right children after recursively mirroring the subtrees?
We swap after recursion to ensure the subtrees themselves are mirrored first, then the current node's children are swapped to complete the mirror effect.
Click to reveal answer
intermediate
What is the time complexity of mirroring a binary tree?
The time complexity is O(n), where n is the number of nodes, because each node is visited once to swap its children.
Click to reveal answer
What happens to the left and right children of a node when mirroring a binary tree?
AThey are duplicated
BThey are swapped
CThey remain the same
DThey are deleted
✗ Incorrect
Mirroring swaps the left and right children of every node.
What is the base case in the recursive mirror function for a binary tree?
ANode is a leaf
BNode has no children
CNode is nil
DNode has two children
✗ Incorrect
The recursion stops when the node is nil (empty).
Which traversal order is used when mirroring a binary tree recursively?
APre-order (root, left, right)
BLevel-order
CIn-order (left, root, right)
DPost-order (left, right, root)
✗ Incorrect
Mirroring uses post-order traversal to first mirror subtrees before swapping children.
What is the time complexity of mirroring a binary tree with n nodes?
AO(n)
BO(log n)
CO(n^2)
DO(1)
✗ Incorrect
Each node is visited once, so time complexity is O(n).
In Go, what type should the left and right children of a binary tree node be?
A*TreeNode
Bint
Cstring
Dbool
✗ Incorrect
Left and right children are pointers to TreeNode structs.
Explain step-by-step how to mirror a binary tree using recursion.
Think about what happens at each node and how recursion helps.
You got /4 concepts.
Write the Go function signature and describe the logic to mirror a binary tree.
Focus on input/output and recursive steps.
You got /4 concepts.
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: