Invert Binary Tree

Easy Subject: Trees
Time Complexity
O(N)
Space Complexity
O(H)

Problem Description

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

Optimal Solution

Python
def invertTree(root):
    if not root:
        return None

    # Swap children
    root.left, root.right = root.right, root.left

    # Recurse
    invertTree(root.left)
    invertTree(root.right)
    return root