You are given the root of a binary tree. Produce the mirror image of that tree by swapping the left and right child of every node, then return the (unchanged) root reference.
Inverting a tree means that at each node the two subtrees trade places, and this swap is applied all the way down to the leaves. After the operation, an in-order walk of the tree reads in reverse compared to the original.
Example 1
Input: root = [4,2,7,1,3,6,9]
Output: [4,7,2,9,6,3,1]
Every node keeps its value, but each node's left and right children are swapped, so 2 and 7 trade places, and beneath them 1/3 and 6/9 trade places as well.
Example 2
Input: root = [2,1,3]
Output: [2,3,1]
The root keeps value 2 while its children 1 and 3 swap sides.
Constraints
The number of nodes in the tree is in the range [0, 100].-100 <= Node.val <= 100See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.
FDE Coach is a cohort-based program in frontend, backend, AWS, and AI where you build real products and get referred to 200+ hiring partners. The free live workshop is the fastest way to see how we teach.
750+ engineers trained · frontend, backend, AWS & AI