easyBfsBinary TreeRecursionTrees 0 views

Symmetric Tree

Determine whether a binary tree is a mirror of itself around its center.

Given the root of a binary tree, check whether it is a mirror of itself around its center (i.e., every left subtree is a mirror reflection of the corresponding right subtree).

For example, this tree is symmetric:

        1
      /   \
     2     2
    / \   / \
   3   4 4   3

This tree is not symmetric -- the left and right subtrees differ in both shape and values, even though each side is individually a valid tree:

                1
              /   \
             2     3
            / \   / \
           4   5 6   7
          /   /     / \
         8   9    10  11
                        \
                        12

Example 1

Input: root = [1,2,2,3,4,4,3]

Output: true

Example 2

Input: root = [1,2,2,null,3,null,3]

Output: false

Example 3

Input: root = []

Output: true

Explanation: An empty tree is trivially symmetric.

Constraints

  • The number of nodes in the tree is in the range [0, 1000].
  • -100 <= Node.val <= 100

Follow-up

Can you solve it both recursively and iteratively?

Hints

Companies

No companies reported yet.

Discussion

Sign in to join the discussion.

Loading discussion...

Test results

No test cases yet.