🎢 ELI5 Park / Trees & Graphs / Invert Binary Tree
Easy

Invert Binary Tree

👶 The 5-Year-Old Summary

Flip a tree like a mirror! Every left child becomes right, every right becomes left! Like swapping left and right hands!

🎮 Interactive Visualizer

Before:

After (in progress):

🧠 The Brain Hack

Recursive swap! For each node: swap its children, then recursively invert left and right. Base case: null node = just return it!

💻 The Clean Solution

def invert_tree(root):
    # Base case: empty tree
    if not root:
        return root
    
    # Swap left and right children
    root.left, root.right = root.right, root.left
    
    # Recursively invert children
    invert_tree(root.left)
    invert_tree(root.right)
    
    return root

#     4         →       4
#    / \               / \
#   2   7             7   2
#  / \               / \
# 1   3             3   1

Time: O(n)

Space: O(h)