/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isMirror(TreeNode* p,TreeNode* q)
{
if(!p&&!q)return true;
else if(!p&&q||p&&!q)return false;
else if(p->val==q->val)return isMirror(p->left,q->right)&&isMirror(p->right,q->left);
else return false;
}
bool isSymmetric(TreeNode* root) {
return isMirror(root,root);
}
};
利用递归和镜像对称,我是看了答案才写出来,不得不说递归思想是真的牛逼。