226. Invert Binary Tree

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

class Solution:
    def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
        def recurse(node):
            if node:
                node.left, node.right = node.right, node.left
                recurse(node.left)
                recurse(node.right)
        
        recurse(root)
        return(root)
  • recurse down the tree and swap at every node.
  • tree is guaranteed to be well-formed.

Categories:: recursion, binary-tree, tree