/** * 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 isSymmetric(TreeNode* root) { if(!root) return true; queue<TreeNode*> q; q.push(root->left); q.push(root->right); while(!q.empty()){ TreeNode* lt=q.front(); q.pop(); TreeNode* rt=q.front(); q.pop(); if(!rt&&!lt) continue; if(!rt||!lt) return false; if(lt->val!=rt->val) return false; q.push(lt->left); q.push(rt->right); q.push(lt->right); q.push(rt->left); } return true; } bool isSymmetricHelper(TreeNode* l,TreeNode* r){ if(!r&&!l) return true; if((!r&&l)||(!l&&r)||(r->val!=l->val)) return false; return isSymmetricHelper(l->left,r->right)&&isSymmetricHelper(l->right,r->left); } };