Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Check if Tree is Symmetric

Medium Acceptance 74.98% Points 30.00

You are given the root of a binary tree. Your task is to determine whether the tree is symmetric around its center.

A binary tree is considered symmetric if:

  • The left subtree is a mirror image of the right subtree.
  • Corresponding nodes on both sides have the same values.
  • The structure of both subtrees is also mirrored.
Examples
Example 1

image Input: root = [1,2,2,3,4,4,3]
Output: true
Explanation: The left and right subtrees are mirror images of each other.

Example 2

image Input: root = [1,2,2,null,3,null,3]
Output: false
Explanation: The left and right subtrees are not mirror images.

Example 3

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

Constraints
    • 0 = Number of nodes = 105
    • -10? = Node.data = 10?
Companies
Atlassian ServiceNow
Topics
Tree Traversal Binary Tree
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions