Answer including proper use of modulus operator


#1
#define mod 1003
 
int sum(TreeNode* A, int val){
    if(A==NULL) return 0;
    val=((val*10)%mod+(A->val)%mod)%mod;
    if(A->right==NULL && A->left==NULL) return val;
    return ((sum(A->left, val))%mod+(sum(A->right, val))%mod)%mod;
}

int Solution::sumNumbers(TreeNode* A){
    if(A==NULL) return 0;
    int ans= sum(A, 0);
    return ans%mod;
}