Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to swap the left and right children of the root node.
DSA Python
def mirror(root): if root is None: return root.left, root.[1] = root.[1], root.left
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping with 'left' instead of 'right' causes no change.
Trying to swap with 'parent' or 'child' which are not valid attributes.
✗ Incorrect
Swapping root.left and root.right mirrors the root node's children.
2fill in blank
mediumComplete the code to recursively mirror the left subtree.
DSA Python
def mirror(root): if root is None: return root.left, root.right = root.right, root.left mirror(root.[1])
Attempts:
3 left
💡 Hint
Common Mistakes
Calling mirror on root.right only misses the left subtree.
Using 'parent' or 'child' which are not valid attributes.
✗ Incorrect
We call mirror on root.left to recursively mirror the left subtree.
3fill in blank
hardFix the error in the recursive call to mirror the right subtree.
DSA Python
def mirror(root): if root is None: return root.left, root.right = root.right, root.left mirror(root.left) mirror(root.[1])
Attempts:
3 left
💡 Hint
Common Mistakes
Calling mirror on root.left twice.
Using invalid attributes like 'parent' or 'child'.
✗ Incorrect
We must call mirror on root.right to mirror the right subtree.
4fill in blank
hardFill both blanks to complete the mirror function that swaps children and recursively mirrors subtrees.
DSA Python
def mirror(root): if root is None: return root.[1], root.[2] = root.[2], root.[1] mirror(root.left) mirror(root.right)
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping with 'parent' or 'child' which are invalid.
Swapping the same attribute with itself.
✗ Incorrect
Swapping root.left and root.right mirrors the node's children correctly.
5fill in blank
hardFill all three blanks to create a dictionary comprehension that maps node values to their mirrored subtree sizes if size > 0.
DSA Python
def subtree_size(node): if node is None: return 0 return 1 + subtree_size(node.left) + subtree_size(node.right) sizes = {node.[1]: subtree_size(node) for node in nodes if subtree_size(node) [2] 0 and node.[3] is not None}
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'val' incorrectly or missing the condition.
Using wrong comparison operators.
Checking the wrong child attribute.
✗ Incorrect
We map node.val to subtree_size(node) if subtree_size(node) > 0 and node.left is not None.
