C++ | using DFS


#1
const int MODVAL = 1003;
void dfs(TreeNode *A, int curVal, long &sum) {
    if(not A) return;
    curVal %= MODVAL;
    if(not A->left and not A->right)
        sum = (sum + (curVal*10 + A->val) % MODVAL) % MODVAL;
    dfs(A->left , curVal*10 + A->val, sum);
    dfs(A->right, curVal*10 + A->val, sum);
}
int Solution::sumNumbers(TreeNode* A) {
    long sum = 0;
    dfs(A, 0, sum);
    return (sum%MODVAL);
}