Determine whether a binary tree is a mirror of itself around its center.
Symmetric Tree
Given the root of a binary tree, determine whether the tree is symmetric around its center.
A binary tree is symmetric if the left subtree is a mirror reflection of the right subtree. In other words, the left and right halves should match in structure and node values when viewed as mirrors.
Return true if the tree is symmetric, otherwise return false.
Key idea
You need to compare nodes in mirrored positions, not just traverse the tree in one order.
Input Format
- A binary tree root node is provided as the input.
- Each node contains an integer value and pointers to
leftandrightchildren.
Output Format
- Return
trueif the tree is symmetric. - Otherwise return
false.
Constraints
- The number of nodes is at least 0.
- Node values may repeat.
- The tree can be empty.
Example 1
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
Input
root = [1,2,2,null,3,null,3]
Output
false
Explanation
The structure differs on the two sides, so the tree is not symmetric.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.