Is the Tree a Mirror Image of Itself
Implement isMirror
You are given the root of a binary tree. Decide whether the tree is a mirror image of itself: if you flipped the left subtree left-to-right, it would have to be exactly the same as the right subtree — same shape, same values. An empty tree counts as a mirror.
Building a flipped copy and comparing works, but the flip and the comparison can be combined: walk the two subtrees together, always pairing the outer children with each other and the inner children with each other.
Example 1:
Input: root = [8,4,4,null,6,6]
Output: true
Example 2:
Input: root = [6,3,3,9,null,9]
Output: false
Example 3:
Input: root = []
Output: true
+ 13 hidden test cases run on Submit.
Constraints:
- ●
0 ≤ number of nodes ≤ 100 - ●
−100 ≤ node.val ≤ 100 - ●
The tree is given as its root node (null for an empty tree); each node has a val, a left child and a right child - ●
The tree is a mirror image of itself when its left subtree, flipped left-to-right, is exactly the same as its right subtree (same shape and same values). Return true or false; an empty tree counts as a mirror
root =
[8, 4, 4, null, 6, 6]