Here is the solution:
function invertTree(root):
if root is null:
return null
temp = root.left
root.left = root.right
root.right = temp
invertTree(root.left)
invertTree(root.right)
return root
You swap the pointers with a temp variable. Then you recurse on the swapped children. The recursion propagates the inversion down the tree. Finally, you return the root.
This runs in time and uses space.