https://leetcode.com/problems/invert-binary-tree

Here what we need to do is just use definition of inverted tree. We go from top to bottom of our tree and if we reached the leaf, we do not do anything. If current subtree is not a leaf, we recursively call our function for both its children, first inverting them.

Complexity

Time complexity is $O(n)$, space complexity** is O(h), where h is the height of our tree.

class Solution:
    def invertTree(self, root):
        if not root: return None
        root.left, root.right = self.invertTree(root.right), self.invertTree(root.left)

        return root

If you like the solution, you can upvote it on leetcode discussion section: Problem 0226