Problem: Given a binary tree, check whether it is mirror-symmetrical.
For example, the binary tree [1,2,2,3,4,4,3] is symmetric.
1
/
22
/ \ /
3443
Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/symmetric-tree
copyrighted by deduction from all networks. For commercial reprints, please contact the official authorization. For non-commercial reprints, please indicate the source.
Code:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
bool is_symmetric(struct TreeNode* root1,struct TreeNode* root2){
if(!root1&&!root2)
return true;
if(!root1||!root2){
return false;
}
if(root1->val==root2->val){
return is_symmetric(root1->left,root2->right)&&is_symmetric(root1->right,root2->left);
}
return false;
}
bool isSymmetric(struct TreeNode* root){
return is_symmetric(root,root);
}