Skip to content
AI360Xpert
Beta
Difficulty: EasyTrees

Invert Binary Tree

Problem in Plain English

Given the root of a binary tree, swap every left and right child so the tree becomes its mirror image, then return the root. An empty tree stays empty.

Problem Statement

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

Inverting means swapping the left and right child of every node, which mirrors the whole tree. An empty tree inverts to itself.

Constraints

  • The number of nodes is between 0 and 100, inclusive.
  • Each node value is between -100 and 100, inclusive; inversion changes child links, not values.

Examples

Example 1

Input
root = [4,2,7,1,3,6,9]
Output
[4,7,2,9,6,3,1]

Root 4 trades children 2 and 7. Within those moved subtrees, 7 trades 6 and 9, and 2 trades 1 and 3. The leaves have no children, so their values stay unchanged while every left/right path reverses.

Example 2

Input
root = [2,1,3]
Output
[2,3,1]

Only root 2 has children to exchange. Nodes 1 and 3 stay childless, so swapping the root already completes this mirror.

Example 3

Input
root = []
Output
[]

The root is null. Both approaches return null immediately without allocating or swapping anything.

Example 4

Input
root = [1,2,null,3,4]
Output
[1,null,2,4,3]

The missing right child of root 1 becomes its missing left child. Node 2 moves to the right and swaps its own children from 3,4 to 4,3. This is a structural mirror with null positions retained, not a reversal of the level-order array.

Example 5

Input
root = [8]
Output
[8]

Swapping two null child references leaves a singleton unchanged in value and shape. The in-place approach returns this same node; the copy approach returns a different node holding 8.

Intuition

Mirroring reverses every left/right choice along a root-to-node path. Swapping only the root moves whole subtrees but leaves their interiors facing the old direction. In Example 1, root 4 trades 2 and 7, then node 7 must also trade 6 and 9 and node 2 must trade 1 and 3. The same local operation applies independently at every node. We can either create a separate mirror with fresh nodes or rewire the original nodes; the in-place version returns the original root.

Approaches

Build Mirrored Copy

Solution Details

Reveal Build Mirrored Copy: intuition, complexity, and code

Hints

Hint 1
At one node, which two references must exchange positions to reverse its immediate left/right choice?
Hint 2
Moving a subtree to the other side does not mirror its interior. Apply the same operation to both child subtrees.
Hint 3
For in-place inversion, save the old left child before overwriting it, recurse into each moved child exactly once, and return the current node.

Edge Cases

  • An empty tree returns null; no new node is created.
  • A singleton keeps its value and null children; the copy approach creates a fresh identity while in-place inversion retains the original.
  • A left-only or right-only chain moves to the opposite side at every level, including all 100 nodes at the stated maximum.
  • Asymmetric trees must mirror missing children too: Example 4 has a null left child after inversion.
  • Duplicate and negative values must be preserved; values cannot identify nodes or determine which subtree to swap.
  • In-place inversion is an involution: running it twice restores all original child references. Copying preserves the input, and every call produces a separate output tree.

Common Mistakes and Interview Tips

  • Assigning left = right before saving old left, then assigning right = left: both references point to the same subtree and the other one is lost.
  • Swapping only the root or only nodes with two nonempty children; a one-child node must move its child to the other side too.
  • Swapping values instead of child references: the shape and null positions would remain wrong on asymmetric trees.
  • Visiting the same subtree twice through changed pointers while skipping the other. Recursing into both new children once, or both saved original children once, is correct.
  • Returning null after a successful swap, or forgetting to return the new root of a mirrored copy.
  • Claiming in-place recursion uses O(1) space; call frames require O(h). Distinguish that from the copy approach’s O(n) allocated output and clarify whether mutation is allowed in the interview.

Key Takeaway

A whole-tree mirror follows from one local left/right exchange at every node. Choose ownership deliberately: fresh mirrored copies preserve the caller’s tree, while in-place swaps reuse all nodes and applying them twice restores the original.