class Solution {
public:
bool isSymmetric(TreeNode* root) {
if(root==NULL) return true;
return sychelp(root->left,root->right);
}
bool sychelp(TreeNode *l,TreeNode *r){
if(!l&&!r) return true;
return l&&r&&l->val==r->val&&sychelp(l->left,r->right)&&sychelp(l->right,r->left);
}
};
bfs
class Solution {
public:
bool isSymmetric(TreeNode* r) {
if(r==NULL) return true;
queue<TreeNode*> q;
q.push(r->left);
q.push(r->right);
while(!q.empty()){
TreeNode *L=q.front();
q.pop();
TreeNode *R=q.front();
q.pop();
if(!L&&!R) continue;
if(!(L&&R)) return false;
if(L->val!=R->val) return false;
q.push(L->left);
q.push(R->right);
q.push(L->right);
q.push(R->left);
}
return true;
}
};