Skip to main content
Back to ChallengesSymmetric TreeEasy 15 min

Symmetric Tree

Given the root of a binary tree, check whether it is a **mirror of itself** (i.e., symmetric around its center).

Examples

Input: root = [1,2,2,3,4,4,3]
Output: true
The tree is symmetric.
Input: root = [1,2,2,null,3,null,3]
Output: false
Not symmetric — asymmetric subtrees.

Constraints

  • Number of nodes in [1, 1000]
  • -100 <= Node.val <= 100

Complexity Analysis

Time
O(n)
Space
O(h)

Test Cases

#1 Symmetric tree
Input: [1,2,2,3,4,4,3]
Expected: true
#2 Asymmetric
Input: [1,2,2,null,3,null,3]
Expected: false
#3 Single node
Input: [1]
Expected: true
#4 Another symmetric
Input: [1,2,2,null,3,3,null]
Expected: true
JavaScript
Output
Click "Run Code" to see output here...