Can anyone tell me what is problem with my code


#1

My all test cases are correct but it fails on submit

public class Solution {
public int sumNumbers(TreeNode A) {
    
return    treePathsSumUtil(A,0)%1003;
}

int treePathsSumUtil(TreeNode node, int val)
{
// Base case
if (node == null)
return 0;

    // Update val 
    val = (val * 10 + node.val); 

    // if current node is leaf, return the current value of val 
    if (node.left == null && node.right == null) 
        return val; 

    // recur sum of values for left and right subtree 
    return treePathsSumUtil(node.left, val) 
            + treePathsSumUtil(node.right, val); 
}

#2

Your val is getting overflowed use modulo 1003 while computing val as well as while computing the sum.

val = (val * 10 + node.val);
treePathsSumUtil(node.left, val) + treePathsSumUtil(node.right, val);