PPractick

DSA interview question

Symmetric Binary Tree

Medium · Data structures · Topics: tree, dfs, recursion

The problem

A logo-design tool builds shapes as binary trees and flags designs that are mirror-symmetric around the vertical axis through the root. A tree is symmetric when its left subtree is a mirror image of its right subtree. Two trees mirror each other when their roots are equal, the first tree's left mirrors the second tree's right, and the first tree's right mirrors the second's left. It's the "identical trees" recursion with the children crossed.

Example: [1, 2, 2, 3, 4, 4, 3] → true; [1, 2, 2, null, 3, null, 3] → false.

Function signature

bool isSymmetric(TreeNode* root);

Test cases

Input (level order)OutputExplanation
[1, 2, 2, 3, 4, 4, 3]true
[1, 2, 2, null, 3, null, 3]falseBoth 3s hang on the right
[1, 2, 2, 2, null, 2]falseValues match but the shape doesn't
[1]true
[]true

(Confirm: both values and structure must mirror; an empty tree is symmetric.)

How interviewers phrase it

Clarifying questions to ask

  1. Must the structure mirror as well as the values? (Yes.)
  2. Is the empty tree symmetric? (Yes.)
  3. How deep can the tree be? (That decides between recursion and a queue.)

Follow-ups to expect

  1. "What exactly is compared?"
  2. "Iterative version?"
  3. "Why can't comparing the inorder traversal with its reverse work?"
  4. "How does it relate to 'invert binary tree'?"
  5. "Complexity?"

Practise questions like this with a real engineer

On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.

Book a free mock interview

Related questions

All 474 dsa interview questions

DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in