226. Invert Binary Tree
Given the root of a binary tree, invert it (swap every left/right child) and return the root.
def invert_tree(root):
if not root:
return None
root.left, root.right = (
invert_tree(root.right),
invert_tree(root.left),
)
return root
Trust that invert_tree already correctly inverts whatever subtree you hand it, then the only work left at this node is swapping its two children. The base case -- an empty subtree inverts to itself -- stops the recursion.