```
class Solution:
def invertTree(self, root):
stack = [root]
while stack:
node = stack.pop()
if node is None:
continue
node.left, node.right = node.right, node.left
stack.append(node.left)
stack.append(node.right)
return root
```

Recursion

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