Repairing a mirror that stops after one level

A file-browser program needs a function that mirrors a binary tree: it exchanges left and right children at every node while preserving each stored label. None represents an empty tree, and Node(label, left, right) constructs a node with the given children.


The following Python function exchanges the root's children but does not transform the subtrees. Repair its final return statement using structural recursion, applying the function to the child structures before constructing the result.


Python
def mirror(t):
    if t is None:
        return None
    return Node(t.label, t.right, t.left)


The diagram gives an example input with branching below the root.


A tree has root A with children B and C, and B has children D and E.


In the corrected return statement, the left-child argument is and the right-child argument is . Write the expressions using the exact function and attribute names shown, without spaces.

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Structural Recursion Questions: