Skip to interview prep
Apexed

18. Mirror (Invert) a Binary Tree

EasyTrees

Convert a binary tree into its mirror image by swapping every node's left and right children. Return the root.

A binary tree is passed as TreeNode objects with val, left and right. The tests write trees in level order, with null for a missing child.

Example 1

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

Constraints

  • 0 ≤ nodes ≤ 100

Tab indents · Ctrl/⌘ + Enter runs · Esc then Tab leaves the editor

Run checks these cases; the expected output comes from the reference solution. Submit judges all 4 hidden and sample tests.

Code runs only in your browser in an isolated sandbox. Progress and submissions are saved on this device.

Mirror (Invert) a Binary Tree — coding interview practice — Apexed