Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping root.Right with root.Right instead of root.Left.
Using incorrect field names like Parent or Child.
✗ Incorrect
Swapping root.Left and root.Right requires accessing root.Left on the right side to complete the swap.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Calling mirror on root.Right twice.
Using incorrect field names.
✗ Incorrect
To mirror the left subtree, call mirror on root.Left recursively.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Calling mirror on root.Left twice.
Using incorrect field names.
✗ Incorrect
To mirror the right subtree, call mirror on root.Right recursively.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Calling mirror on the same child twice.
Swapping children before recursive calls.
✗ Incorrect
The function must recursively call mirror on both left and right children before swapping them.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect base case condition.
Swapping children before recursive calls.
✗ Incorrect
The base case checks if root is nil, then recursively mirror left and right children before swapping.