Very simple 4 line C++ solution


#1
  1. void travel(TreeNode* A,int sum,int &ans){
  2. if(A==NULL) return;
  3. sum=(sum*10)%1003;sum=(sum+A->val)%1003;
  4. if(A->left==NULL && A->right==NULL) {ans=(ans+sum)%1003; return;}
  5. travel(A->right,sum,ans);
  6. travel(A->left,sum,ans);
  7. }
  • int Solution::sumNumbers(TreeNode* A) {
  1. int sum=0,ans=0;
  2. travel(A,sum,ans);
  3. return ans%1003;
  4. }