noodleProblems/
Symmetric Tree
#132

Symmetric Tree

AlgorithmeasyTreeDepth First SearchBreadth First SearchBinary Tree

Given the root of a binary tree, return true if it is a **mirror of itself** — symmetric around its center.

A tree is symmetric when its left subtree is the mirror image of its right subtree: matched nodes hold equal values, and a left child on one side corresponds to a right child on the other.

The tree is given as a level-order array where null marks a missing child: [1, 2, 2, 3, 4, 4, 3] is symmetric.

Example cases

  • symmetric
    in root = [1,2,2,3,4,4,3]3241423
    out true
    The left subtree mirrors the right: 2=2, 3↔3, 4↔4.
  • asymmetric
    in root = [1,2,2,null,3,null,3]23123
    out false
    Both 2s have a right child but no left child, so they don't mirror.
  • single node
    in root = [1]1
    out true

Constraints

  • The number of nodes in the tree is in the range [1, 1000].
  • -100 <= Node.val <= 100
Saved
root =
[1,2,2,3,4,4,3]