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 Java
TreeNode temp = root.left;
root.left = root.[1];
root.right = temp;Attempts:
3 left
💡 Hint
Common Mistakes
Using root.left instead of root.right causes no swap.
Trying to swap with a non-existent property.
✗ Incorrect
Swapping root.left with root.right requires accessing root.right to assign it to root.left.
2fill in blank
mediumComplete the code to recursively call mirror on the left subtree.
DSA Java
if (root != null) { mirror(root.[1]); }
Attempts:
3 left
💡 Hint
Common Mistakes
Calling mirror on root.right instead of root.left only mirrors half the tree.
Calling mirror on a non-existent property.
✗ Incorrect
To mirror the left subtree, call mirror on root.left.
3fill in blank
hardFix the error in the recursive call to mirror the right subtree.
DSA Java
if (root != null) { mirror(root.left); mirror(root.[1]); }
Attempts:
3 left
💡 Hint
Common Mistakes
Calling mirror on root.left twice.
Using an incorrect property name.
✗ Incorrect
To mirror the right subtree, call mirror on root.right.
4fill in blank
hardFill both blanks to complete the swap and recursive calls for mirroring.
DSA Java
TreeNode temp = root.[1]; root.[2] = root.right; root.right = temp; mirror(root.left); mirror(root.right);
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the wrong child nodes.
Not swapping before recursive calls.
✗ Incorrect
Swap root.left with root.right by storing root.left in temp and assigning root.right to root.left.
5fill in blank
hardFill all three blanks to complete the mirror function with base case and recursive calls.
DSA Java
public void mirror(TreeNode [1]) { if ([2] == null) return; TreeNode temp = [2].left; [2].left = [2].right; [2].right = temp; mirror([2].left); mirror([2].right); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names causing errors.
Missing the base case check.
✗ Incorrect
The parameter and all references inside the function use the same variable name 'root' for clarity.
