Beautiful Recursive and Iterative Solutions
Very simple ideas. Notice how both look similar to each other.\n\n    /\n     * Definition for binary tree\n     * struct TreeNode {\n     *     int val;\n     
Very simple ideas. Notice how both look similar to each other.\n\n    /**\n     * Definition for binary tree\n     * struct TreeNode {\n     *     int val;\n     *     TreeNode *left;\n     *     TreeNode *right;\n     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}\n     * };\n     */\n    #include<queue>\n    using namespace std;\n    typedef pair<TreeNode*,TreeNode*> nodepair;\n    class Solution {\n    public:\n        bool isSymmetricRecursive(TreeNode*a,TreeNode*b){\n            if(a){\n                return b && a->val==b->val && \n                    isSymmetricRecursive(a->left,b->right) &&\n                    isSymmetricRecursive(a->right,b->left);\n            }\n            return !b;\n        }\n        bool isSymmetricRecursive(TreeNode*root){\n            return !root || isSymmetricRecursive(root->left,root->right);\n        }\n        bool isSymmetric(TreeNode *root) {\n            // Level-order BFS.\n            queue<nodepair> q;\n            if(root)\n                q.push(make_pair(root->left,root->right));\n            while(q.size()){\n                nodepair p=q.front(); q.pop();\n                if(p.first){\n                    if(!p.second)return false;\n                    if(p.first->val != p.second->val) return false;\n                    // the order of children pushed to q is the key to the solution.\n                    q.push(make_pair(p.first->left,p.second->right));\n                    q.push(make_pair(p.first->right,p.second->left));\n                }\n                else if(p.second) return false;\n            }\n            return true;\n        }\n    };
[]