class Solution {
public:
bool compare(TreeNode* left, TreeNode* right) {
if(left == nullptr && right == nullptr) return true;
if(left != nullptr && right == nullptr) return false;
if(left == nullptr && right != nullptr) return false;
if(left->val != right->val) return false;
bool c1 = compare(left->left, right->right);
bool c2 = compare(left->right, right->left);
return c1 && c2;
}
bool isSymmetric(TreeNode* root) {
if(root == nullptr) return true;
return compare(root->left, root->right);
}
};
class Solution {
public:
bool isSymmetric(TreeNode* root) {
if(root == nullptr) return true;
queue<TreeNode*> que;
que.push(root->left);
que.push(root->right);
while(!que.empty()) {
TreeNode* left = que.front(); que.pop();
TreeNode* right = que.front(); que.pop();
if(left == nullptr && right == nullptr) continue;
if(left != nullptr && right == nullptr) return false;
if(left == nullptr && right != nullptr) return false;
if(left->val != right->val) return false;
que.push(left->left);
que.push(right->right);
que.push(left->right);
que.push(right->left);
}
return true;
}
};