Skip to content
BytePatterns

Mirror Symmetry Check

EasyTrees & BST#recursion#paired-traversal~20m

Problem

Decide whether a binary tree is a mirror image of itself, as if a vertical line ran down through the root. Both the shape and the values have to match across that line. An empty tree counts as symmetric.

Examples

Input:  tree = 1, left 2 with children 3 and 4, right 2 with children 4 and 3
Output: True
Why:    the right subtree is the left subtree read back to front
Input:  tree = 1, left 2 with right child 3, right 2 with right child 3
Output: False
Why:    the two threes hang on the same side, so the shape is not mirrored
Input:  tree = empty
Output: True
Why:    edge case, nothing is trivially symmetric

Hints

0 / 3

Stuck on the idea rather than the code? Binary Trees covers it.