Short 4ms C++ solution with recursion


  • 0
    D
    class Solution {
    public:
        bool isSymmetric(TreeNode* root) {
            return !root ? true : isSymmetricHelper(root->left, root->right);
        }
        
        bool isSymmetricHelper(TreeNode* left, TreeNode* right) {
            if (!left && !right) { return true; }
            return 
                (left && right) &&
                (left->val == right->val) &&
                isSymmetricHelper(left->left, right->right) &&
                isSymmetricHelper(left->right, right->left);
        }
    };

Log in to reply
 

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