v9.1.1
Menu

Symmetric Tree

easyFREETrees & BFSAmazon DSA

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

Example

Input
1,2,2,3,4,4,3
Output
True
Reasoning
  • The input array 1,2,2,3,4,4,3 represents a binary tree in level-order traversal, which can be visualized as: 1 /
    2 2 / \ /
    3 4 4 3
  • To check for symmetry, we compare the left and right subtrees.
  • The left subtree is a mirror of the right subtree: both have the same structure and node values when reflected around the center.
  • Since the tree is symmetric around its center, the function returns True indicating that the tree is a mirror of itself.

Constraints

  • 1 <= number of nodes <= 1000
  • -100 <= Node.val <= 100

Learn this in

More from Amazon DSA

solution.py

Test results

Sign in to run your code against the test cases.