Symmetric Tree Easy 0 attempts
LeetCode ↗

Symmetric Tree

Easy TreeBinary TreeDFS LeetCode

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

Example: root = [1,2,2,3,4,4,3] → true

Sample Input
Sample Output
Constraints
  • 1 <= number of nodes <= 1000
  • -100 <= Node.val <= 100

Recursive Mirror Check

function isSymmetric(root) {
  function isMirror(a, b) {
    if (!a && !b) return true;
    if (!a || !b || a.val !== b.val) return false;
    return isMirror(a.left, b.right) && isMirror(a.right, b.left);
  }
  return isMirror(root, root);
}

Time: O(n) | Space: O(h)

Saved in this browser only. Private to you.

JavaScript