Data Structures19 sections · 729 units
Open in Course

Problem - Invert Binary Tree

Mirror the tree

Given the root of a binary tree, invert the tree and return its root. Inverting means swapping left and right children at every node. The result is a mirror image.

Example: [4,2,7,1,3,6,9][4,2,7,1,3,6,9] becomes [4,7,2,9,6,3,1][4,7,2,9,6,3,1]. This is famously the problem that inspired a Homebrew developer to tweet about failing a Google interview.

Despite its simplicity, it tests whether you understand tree recursion. Constraints: up to 100100 nodes, values from 100-100 to 100100.