题目
1 class Solution { 2 public: 3 4 bool isSymmetric(TreeNode* root) { 5 return check(root,root); 6 } 7 bool check(TreeNode* p,TreeNode* q) { 8 if(!p && !q ) return true; 9 if(!p || !q ) return false; 10 return (p->val == q->val) && check(p->left,q->right) && check(p->right,q->left); 11 } 12 };
1 class Solution { 2 public: 3 4 bool isSymmetric(TreeNode* root) { 5 return check(root,root); 6 } 7 bool check(TreeNode* p,TreeNode* q) { 8 queue<TreeNode*>queue1; 9 queue1.push(p);queue1.push(q); 10 while(!queue1.empty()) { 11 p = queue1.front();queue1.pop(); 12 q = queue1.front();queue1.pop(); 13 14 if(!p && !q) continue; 15 if((!p || !q) || p->val != q->val) return false; 16 17 queue1.push(p->left); 18 queue1.push(q->right); 19 queue1.push(p->right); 20 queue1.push(q->left); 21 22 } 23 return true; 24 } 25 };