Is the Tree a Mirror Image of Itself

Solve this Problem
Easy15–20 min
Topics
Companies

You are given the root of a binary tree. Decide whether the tree is a mirror image of itself: if you flipped the left subtree left-to-right, it would have to be exactly the same as the right subtree — same shape, same values. An empty tree counts as a mirror.

Building a flipped copy and comparing works, but the flip and the comparison can be combined: walk the two subtrees together, always pairing the outer children with each other and the inner children with each other.

Test Case 1:

Input:root = [8, 4, 4, null, 6, 6]
Output:true
Explanation:Below the root, both children hold 4. The left 4 has a child 6 on its right; the right 4 has a child 6 on its left — exactly the mirror position.

Test Case 2:

Input:root = [6, 3, 3, 9, null, 9]
Output:false
Explanation:Both 3s have a child 9, but on the same side: the 9 under the left 3 is on its left, and so is the 9 under the right 3. In a mirror it would have to be on the right.

Test Case 3:

Input:root = []
Output:true
Explanation:An empty tree is trivially symmetric.

Constraints

  • ◆0 ≤ number of nodes ≤ 100
  • ◆−100 ≤ node.val ≤ 100
  • ◆The tree is given as its root node (null for an empty tree); each node has a val, a left child and a right child
  • ◆The tree is a mirror image of itself when its left subtree, flipped left-to-right, is exactly the same as its right subtree (same shape and same values). Return true or false; an empty tree counts as a mirror
🚀

Try the Dry Run

Don't just read the solution — watch it execute, one step at a time.

Approach & Solutions

Brute Force — Build the Flipped Left Subtree, Then Compare

Brute

Follow the definition literally. Build a flipped copy of the root's left subtree — for every node in the copy, its left child is the flipped copy of the original's right child and vice versa. Then check whether that flipped copy is exactly the same tree as the root's right subtree, comparing shape and values node by node. It is correct and easy to reason about, but it allocates a whole new subtree (O(n) extra space) before the comparison even begins.

TimeO(n)
SpaceO(n)
1class Solution { 2 public boolean isMirror(TreeNode root) { 3 if (root == null) return true; 4 return same(mirrorCopy(root.left), root.right); 5 } 6 7 private TreeNode mirrorCopy(TreeNode node) { 8 if (node == null) return null; 9 TreeNode copy = new TreeNode(node.val); 10 copy.left = mirrorCopy(node.right); 11 copy.right = mirrorCopy(node.left); 12 return copy; 13 } 14 15 private boolean same(TreeNode x, TreeNode y) { 16 if (x == null && y == null) return true; 17 if (x == null || y == null) return false; 18 return x.val == y.val && same(x.left, y.left) && same(x.right, y.right); 19 } 20}

Optimal — Compare Mirror Positions in One Pass

Optimal

Skip the copy. Walk the left and right subtrees at the same time with a helper that takes two nodes, a and b, that should be mirror images. Both missing → fine. Only one missing → not a mirror. Otherwise their values must be equal, and the mirror rule pairs the OUTER children (a's left with b's right) and the INNER children (a's right with b's left). All three conditions joined with && short-circuit at the first mismatch. No copy is built: O(n) time in the worst case, O(h) extra space for the recursion.

TimeO(n)
SpaceO(h)
1class Solution { 2 public boolean isMirror(TreeNode root) { 3 return root == null || mirror(root.left, root.right); 4 } 5 6 private boolean mirror(TreeNode a, TreeNode b) { 7 if (a == null && b == null) return true; 8 if (a == null || b == null) return false; 9 return a.val == b.val && mirror(a.left, b.right) && mirror(a.right, b.left); 10 } 11}

Related Problems