C++ elegant code like digit dp using dfs


#1
    int M=1003,S;
    void dfs(TreeNode* A,int sum=0){
        if(A->left==NULL and A->right==NULL){
            int ans=(sum*10+A->val)%M;
            S=(ans+S)%M;
            return;
        }
        sum=(sum*10+A->val)%M;
        if(A->left!=NULL)dfs(A->left,sum);
        if(A->right!=NULL)dfs(A->right,sum);
    }
    int Solution::sumNumbers(TreeNode* A) {
        S=0;dfs(A,0);
        return S;
    }