Symmetric Binary Tree
Problem statement
Given the root of a binary tree, return true when the tree is symmetric around its center and false otherwise.
Two subtrees mirror each other when their root values are equal, the left subtree of one mirrors the right subtree of the other, and the right subtree of one mirrors the left subtree of the other.
Function
isSymmetric(root: TreeNode) → booleanExamples
Example 1
root = [1,2,2,3,4,4,3]return = trueThe left and right subtrees have equal values in mirrored positions.
Example 2
root = [1,2,2,null,3,null,3]return = falseThe two nodes with value 3 occupy the same side of their parents instead of mirrored sides.
Example 3
root = []return = trueAn empty tree has no mismatched pair, so it is symmetric.
Constraints
- The tree contains at most
10^5nodes. - Each node value fits in a signed 32-bit integer.
- The empty tree is symmetric.