Check if a Binary Tree is Symmetric
Problem Write a function that takes the root of a binary tree and returns whether the tree is symmetric — a mirror image of itself around its centre.
Input / Output
- Input:
root, the root node of a binary tree. - Output: true if the tree is a mirror of itself, false otherwise.
Constraints
- The tree may be empty, which counts as symmetric.
- Symmetry is structural AND by value: the shape must mirror and the mirrored node values must match.
- The tree may be arbitrarily shaped and unbalanced, so recursion depth can reach O(n) in the worst case.
Example
- Root 1 with children [2,2] and grandchildren [3,4,4,3] → symmetric.
- Root 1 with children [2,2] where each 2 has only a right child 3 → NOT symmetric: the values read the same left to right, but the structure does not mirror. This defeats the shortcut of comparing an inorder traversal against its reverse.
asked …