递归

class Solution {
private:
    bool compare(TreeNode* a, TreeNode* b) {
        if (!a && !b) return true;
        if (!a || !b) return false;
        if (a->val != b->val) return false;

        bool outsider = compare(a->left, b->right);
        bool insider = compare(a->right, b->left);
        return outsider && insider;
    }
public:
    bool isSymmetric(TreeNode* root) {
        return compare(root, root);
    }
};

迭代

class Solution {
public:
    bool isSymmetric(TreeNode* root) {
        queue<TreeNode*> q;
        TreeNode *l, *r;
        q.push(root), q.push(root);
        while (!q.empty()) {
            l = q.front(); q.pop();
            r = q.front(); q.pop();
            if (l == nullptr && r == nullptr) continue;
            if (l == nullptr || r == nullptr) return false;
            if (l->val != r->val) return false;
            
						// outside
            q.push(l->left);
            q.push(r->right);
						// inside
            q.push(l->right);
            q.push(r->left);

        }
        return true;
    }
};