[Help] C++ Why does my code not give correct result?


  • 0
    C

    Any ideas are appreciated. Thx!

    class Solution {
    public:
        int pathSum(TreeNode* root, int sum) {
            if (root == NULL)
                return 0;
            int a = 0, b = 0, c = 0;
            if (root -> left != NULL)
            {
                a = pathSum(root -> left, sum - root -> val);
                if (root -> val != 0)
                    a += pathSum(root -> left, sum);
            }
            if (root -> right != NULL)
            {
                b = pathSum(root -> right, sum - root -> val);
                if (root -> val != 0)
                    b += pathSum(root -> right, sum);
            }
            if (root -> val == sum)
                c = 1;
            return  a + b + c;
        }
    };
    

Log in to reply
 

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