Invert Binary Tree

Med
Code
Loading editor...
Tap Analyze to see visualization
Variables

Run code to see variables

Output

Console output will appear here

Press Space to start•← → to step•? all shortcuts

Solution Guide: Invert Binary Tree

Approach

Recursively invert children first, then swap node.left and node.right.

Complexity Analysis

Time
O(n) where n is node count
Space
O(h) where h is tree height

Pattern

Tree Transformation

Why It Works

Every node is independent: swapping both children at each node flips the entire subtree.

Updated Mar 2026