Problem
Determine whether a binary tree is symmetric about its root: its left and right sides must be mirror images with equal values in corresponding reflected positions. Both shape and values matter.
Use the supplied TreeNode fields val, left, and right:
bool isSymmetric(TreeNode* root);
Level-order examples use null for missing children; the actual argument is a node pointer.
Examples
Input: root = [5,2,2,1,4,4,1]
Output: true
Input: root = [6,3,3,null,2,null,2]
Output: false
The two nodes valued 2 both occupy right-child positions, rather than reflected positions.
Constraints
-
The tree contains between 1 and 1000 nodes.
-
Node values lie between -100 and 100, inclusive.
Source follow-up
Consider both recursive and iterative solutions.