Bird
Raised Fist0
DSA Goprogramming~20 mins

Mirror a Binary Tree in DSA Go - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Mirror Tree Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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)
}
A1 2 3 4 5
B1 3 2 4 5
C1 2 3 5 4
D1 3 2 5 4
Attempts:
2 left
💡 Hint
Think about swapping left and right children at every node.
🧠 Conceptual
intermediate
1:30remaining
Understanding Mirror Operation on Binary Trees
Which statement correctly describes the effect of the mirror operation on a binary tree?
AIt swaps the left and right children of every node in the tree.
BIt removes all leaf nodes from the tree.
CIt reverses the order of nodes in the inorder traversal only.
DIt duplicates every node and attaches the duplicate as the left child.
Attempts:
2 left
💡 Hint
Think about what happens to each node's children after mirroring.
🔧 Debug
advanced
2: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
}
ACompilation error: missing return statement.
BNo error; code runs correctly and mirrors the tree.
CNil pointer dereference runtime error.
DStack overflow due to infinite recursion.
Attempts:
2 left
💡 Hint
Check the base case and recursive calls carefully.
❓ Predict Output
advanced
2: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)
}
A1 2 3 4
B1 2 3
C1 4 3 2
D1 4 3
Attempts:
2 left
💡 Hint
Mirroring a right-skewed tree makes it left-skewed.
🧠 Conceptual
expert
2:30remaining
Effect of Mirroring on Tree Height and Structure
After mirroring a binary tree, which of the following statements is always true?
AThe height of the tree remains the same, but the left and right subtrees are swapped at every node.
BThe height of the tree doubles because nodes are duplicated.
CThe tree becomes a complete binary tree regardless of original shape.
DAll leaf nodes become internal nodes after mirroring.
Attempts:
2 left
💡 Hint
Mirroring flips the tree horizontally but does not add or remove nodes.

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

  1. Step 1: Understand the mirror operation

    Mirroring a binary tree means swapping the left and right children of every node.
  2. Step 2: Apply the definition to the tree

    By swapping children at every node, the tree becomes a mirror image of the original.
  3. Final Answer:

    Swap the left and right children of every node in the tree -> Option A
  4. 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

  1. Step 1: Understand pointer usage in Go trees

    Binary trees are usually represented with pointers to nodes to allow modification.
  2. Step 2: Check return type for mirrored tree

    The function should return a pointer to the root of the mirrored tree.
  3. Final Answer:

    func mirrorTree(root *TreeNode) *TreeNode -> Option D
  4. 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

  1. Step 1: Understand the initial tree structure

    Root is 1, left child is 2, right child is 3.
  2. Step 2: Apply mirrorTree function

    Mirror swaps left and right children at root, so left becomes 3 and right becomes 2.
  3. Final Answer:

    1 3 2 -> Option C
  4. 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:
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
}
medium
A. The base case is missing
B. The recursive calls do not assign the mirrored subtrees back to root.Left and root.Right
C. The function returns the wrong type
D. Swapping children should happen before recursive calls

Solution

  1. Step 1: Analyze recursive calls

    The function calls mirrorTree on root.Left and root.Right but ignores their returned mirrored subtrees.
  2. Step 2: Check assignment of mirrored subtrees

    Without assigning returned values, the subtrees are not updated, so mirror is incomplete.
  3. Final Answer:

    The recursive calls do not assign the mirrored subtrees back to root.Left and root.Right -> Option B
  4. Quick Check:

    Assign returned mirrored subtrees to children [OK]
Hint: Always assign recursive results back to node children [OK]
Common Mistakes:
  • Ignoring returned subtree pointers
  • Swapping children before recursion
  • Missing base case (though present here)
5. Given a binary tree, how would you modify the mirror function to also count how many nodes were swapped during the mirroring process?
hard
A. Add a counter variable and increment it each time left and right children are swapped recursively
B. Return the sum of node values instead of mirroring
C. Only swap nodes if their values are even
D. Use a queue to traverse the tree without recursion

Solution

  1. Step 1: Understand the goal

    We want to count how many nodes had their children swapped during mirroring.
  2. 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.
  3. Final Answer:

    Add a counter variable and increment it each time left and right children are swapped recursively -> Option A
  4. Quick Check:

    Count swaps during recursion [OK]
Hint: Count swaps inside recursion when swapping children [OK]
Common Mistakes:
  • Counting node values instead of swaps
  • Not accumulating counts from recursive calls
  • Using traversal without swapping
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard