Skip to content
EasyTreesAI interview only

Symmetric Tree

Asked atamazonmicrosoftbloomberglinkedinmetagoogle

01 · Problem

Given the root of a binary tree, determine whether it is symmetric, meaning the left half is an exact mirror reflection of the right half across a vertical line through the root (both in shape and in values).

Return true if it is symmetric and false otherwise. The tree is given in level-order, with null marking a missing child. A single node is symmetric, and an empty tree is considered symmetric.

02 · Examples

Example 01
      1
    /   \
   2     2
  / \   / \
 3   4 4   3
Input
root = [1,2,2,3,4,4,3]
Output
true

The left subtree [2,3,4] mirrors the right subtree [2,4,3].

Example 02
Input
root = [1,2,2,null,3,null,3]
Output
false

Both 3s are right children; a mirror would need one of them to be a left child.

Example 03
Input
root = [1]
Output
true

A lone root is trivially its own mirror.

03 · Constraints

  • 01The number of nodes in the tree is in the range [0, 100].
  • 02-100 <= Node.val <= 100
  • 03Node values may repeat, so symmetry must be checked by position, not by value alone.
  • 04The tree height is at most 100, and the tree may be fully skewed.

04 · Optimal complexity

Time
O(n)
Space
O(h)
05 · Two ways to work on it

Practice it alone or rehearse it as an interview.

Practice Mode gives you an editor and test runs, nothing else. AI Interview Mode puts a voice interviewer on the other side, adds a clock, and ends with a scored summary of the round.