Given a binary tree, check whether it is a mirror of the itself (ie, symmetric around its center).
For example, this binary tree is [1,2,2,3,4,4,3] symmetric:
1 / 2 2/\/3 4 4 3
But the following are not [1,2,2,null,3,null,3] :
1 / 2 2 \ 3 3
Note:
Bonus points if you could solve it both recursively and iteratively
/** Definition for a binary tree node. * struct TreeNode {* int val; * TreeNode *left; * TreeNode *right ; * TreeNode (int x): Val (x), left (null), right (NULL) {} *}; */classSolution { Public: BOOLIssymmetric (TreeNode *lhs, TreeNode *RHS) { if(NULL = = Lhs&&null = = RHS)return true; if(Null!=lhs&&null!=rhs&&lhs->val = = rhs->val) { returnIssymmetric (Lhs->right, Rhs->left) && issymmetric (Lhs->left, rhs->Right ); } return false; } BOOLIssymmetric (treenode*root) { if(!root)return true; returnIssymmetric (Root->left, root->Right ); }};
Leetcode 101. Symmetric Tree