0
0
DSA Goprogramming~10 mins

Mirror a Binary Tree in DSA Go - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to swap the left and right child nodes of the root.

DSA Go
func mirror(root *Node) {
    if root == nil {
        return
    }
    root.Left, root.Right = root.Right, root.[1]
}
Drag options to blanks, or click blank then click option'
ALeft
BRight
CParent
DChild
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping root.Right with root.Right instead of root.Left.
Using incorrect field names like Parent or Child.
2fill in blank
medium

Complete the code to recursively mirror the left subtree.

DSA Go
func mirror(root *Node) {
    if root == nil {
        return
    }
    mirror(root.[1])
    mirror(root.Right)
    root.Left, root.Right = root.Right, root.Left
}
Drag options to blanks, or click blank then click option'
AChild
BRight
CParent
DLeft
Attempts:
3 left
💡 Hint
Common Mistakes
Calling mirror on root.Right twice.
Using incorrect field names.
3fill in blank
hard

Fix the error in the recursive call to mirror the right subtree.

DSA Go
func mirror(root *Node) {
    if root == nil {
        return
    }
    mirror(root.Left)
    mirror(root.[1])
    root.Left, root.Right = root.Right, root.Left
}
Drag options to blanks, or click blank then click option'
ARight
BLeft
CParent
DChild
Attempts:
3 left
💡 Hint
Common Mistakes
Calling mirror on root.Left twice.
Using incorrect field names.
4fill in blank
hard

Fill both blanks to complete the mirror function that recursively mirrors the tree.

DSA Go
func mirror(root *Node) {
    if root == nil {
        return
    }
    mirror(root.[1])
    mirror(root.[2])
    root.Left, root.Right = root.Right, root.Left
}
Drag options to blanks, or click blank then click option'
ALeft
BRight
CParent
DChild
Attempts:
3 left
💡 Hint
Common Mistakes
Calling mirror on the same child twice.
Swapping children before recursive calls.
5fill in blank
hard

Fill all three blanks to implement a complete mirror function with base case, recursive calls, and swap.

DSA Go
func mirror(root *Node) {
    if root == [1] {
        return
    }
    mirror(root.[2])
    mirror(root.[3])
    root.Left, root.Right = root.Right, root.Left
}
Drag options to blanks, or click blank then click option'
Anil
BLeft
CRight
Droot
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect base case condition.
Swapping children before recursive calls.