Skip to the code
cf.
+10 XP

#319 Mirror-Symmetric Dashboard

easy

A design system checks that a dashboard layout looks the same when flipped left to right. The layout is a binary tree of widget sizes.

Write isSymmetric(root) that returns true if the tree is a mirror image of itself around its center, and false otherwise. An empty tree is symmetric.

Example 1

Input: root = [7,4,4,1,9,9,1]
Output: true

The left side 4 (1, 9) mirrors the right side 4 (9, 1).

Example 2

Input: root = [7,4,4,null,1,null,1]
Output: false

Both 1s are right children, so the sides don't mirror each other.

Constraints

  • 0 <= number of widgets <= 10000
  • 1 <= size <= 100

Trees are written level by level, like [1, 2, 3, null, 4], where null is a missing child. Your function works with TreeNode objects, each with val, left, and right.

Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).