Challenge - 5 Problems
Mirror Tree Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Mirroring a Simple Binary Tree
What is the output of the following Go code after mirroring the binary tree? The output prints the tree nodes in preorder (root, left, right).
DSA Go
package main import "fmt" type Node struct { Val int Left *Node Right *Node } func mirror(root *Node) *Node { if root == nil { return nil } root.Left, root.Right = mirror(root.Right), mirror(root.Left) return root } func preorder(root *Node) { if root == nil { return } fmt.Print(root.Val, " ") preorder(root.Left) preorder(root.Right) } func main() { root := &Node{Val: 1} root.Left = &Node{Val: 2} root.Right = &Node{Val: 3} root.Left.Left = &Node{Val: 4} root.Left.Right = &Node{Val: 5} mirror(root) preorder(root) }
Attempts:
2 left
💡 Hint
Think about swapping left and right children at every node.
✗ Incorrect
Mirroring swaps left and right children recursively. The original preorder is 1 2 4 5 3. After mirroring, left and right children swap, so preorder becomes 1 3 2 5 4.
🧠 Conceptual
intermediate1:30remaining
Understanding Mirror Operation on Binary Trees
Which statement correctly describes the effect of the mirror operation on a binary tree?
Attempts:
2 left
💡 Hint
Think about what happens to each node's children after mirroring.
✗ Incorrect
Mirroring a binary tree means swapping the left and right children of every node recursively. It does not remove or duplicate nodes, nor does it only affect traversal order.
🔧 Debug
advanced2:00remaining
Identify the Error in Mirror Function Implementation
What error does the following Go code produce when trying to mirror a binary tree?
DSA Go
func mirror(root *Node) *Node {
if root == nil {
return root
}
temp := root.Left
root.Left = root.Right
root.Right = temp
mirror(root.Left)
mirror(root.Right)
return root
}Attempts:
2 left
💡 Hint
Check the base case and recursive calls carefully.
✗ Incorrect
The code correctly swaps left and right children and recursively calls mirror on them. The base case returns root when nil, preventing infinite recursion. No errors occur.
❓ Predict Output
advanced2:00remaining
Output After Mirroring a Skewed Binary Tree
Given the following skewed binary tree and mirror function, what is the preorder output after mirroring?
DSA Go
package main import "fmt" type Node struct { Val int Left *Node Right *Node } func mirror(root *Node) *Node { if root == nil { return nil } root.Left, root.Right = mirror(root.Right), mirror(root.Left) return root } func preorder(root *Node) { if root == nil { return } fmt.Print(root.Val, " ") preorder(root.Left) preorder(root.Right) } func main() { root := &Node{Val: 1} root.Right = &Node{Val: 2} root.Right.Right = &Node{Val: 3} root.Right.Right.Right = &Node{Val: 4} mirror(root) preorder(root) }
Attempts:
2 left
💡 Hint
Mirroring a right-skewed tree makes it left-skewed.
✗ Incorrect
The original tree is skewed right: 1 -> 2 -> 3 -> 4. After mirroring, it becomes skewed left: 1 -> 4 -> 3 -> 2. Preorder prints root, left, right, so output is 1 4 3 2.
🧠 Conceptual
expert2:30remaining
Effect of Mirroring on Tree Height and Structure
After mirroring a binary tree, which of the following statements is always true?
Attempts:
2 left
💡 Hint
Mirroring flips the tree horizontally but does not add or remove nodes.
✗ Incorrect
Mirroring swaps left and right children at every node, preserving the height and node count. It does not duplicate nodes or change completeness or leaf status.