Clean C++ solution,easy to understand.


  • 4
    S
    class Solution {
    public:
    
        bool isSymmetricHelper(TreeNode* l,TreeNode* r){
            if(!l && !r) return true;
            if((l&&!r) || (!l&&r) || (l->val != r->val) )  return false;
            return isSymmetricHelper(l->left,r->right) & isSymmetricHelper(l->right,r->left);
        }
        bool isSymmetric(TreeNode* root) {
            if(!root)  return true;
            return isSymmetricHelper(root->left,root->right);
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.