Skip to content

Invert Binary Tree

01 · Question

Given the root of a binary tree, invert the tree, and return its root.

02 · Solution

Reference solution

1def invertTree(root: Optional[TreeNode]) -> Optional[TreeNode]:
2 if not root:
3 return None
4 root.left, root.right = root.right, root.left
5 invertTree(root.left)
6 invertTree(root.right)
7 return root