C++ DFS approach


#1
void helper(TreeNode *root,int tmp,int &sum)
{
    if(!root)
    {
        return;
    }
    if(!root->left && !root->right)
    {
        sum+=(10*tmp)%1003+(root->val%1003);
        return;
    }
    helper(root->left,(10*tmp+root->val)%1003,sum);
    helper(root->right,(10*tmp+root->val)%1003,sum);
}
int Solution::sumNumbers(TreeNode* A) 
{
    if(!A)
    {
        return 0;
    }
    int sum=0;
    helper(A,0,sum);
    return sum%1003;
}