Recursive Solution C++


  • 0
    R
    bool isSymmetric(TreeNode* root) {
        if(root == NULL)
            return true;
        return checkSimilarity(root->left,root->right);
    }
    
    bool checkSimilarity(TreeNode* root1, TreeNode* root2){
        if(root1==NULL&&root2==NULL)
            return true;
        if(root1==NULL&&root2!=NULL||root1!=NULL&&root2==NULL||root1->val!=root2->val)
            return false;
        return checkSimilarity(root1->right,root2->left)&&checkSimilarity(root2->right,root1->left);
    }

Log in to reply
 

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