Backtracking approach


#1

void sumleaf(TreeNode root,long long sum,long long &ans)
{
if(root==NULL)
return ;
sum=(sum
10+root->val)%1003;
if(root->left==NULL && root->right==NULL)
{
ans=ans+sum;
sum=sum/10;
return;
}

sumleaf(root->left,sum,ans);
sumleaf(root->right,sum,ans);
sum=sum/10;

}
int Solution::sumNumbers(TreeNode* root) {
long long ans=0;
sumleaf(root,0,ans);
return ans%1003;
}


#2

Since you are passing the sum variable by value, the expression sum=sum/10 has no effect or use. You can remove it and code will work the same.