Works Fine by use of strings


#1

/**

  • Definition for binary tree
  • struct TreeNode {
  • int val;
    
  • TreeNode *left;
    
  • TreeNode *right;
    
  • TreeNode(int x) : val(x), left(NULL), right(NULL) {}
    
  • };
    */

int getMod(string input)
{
int s,k=0;
for(int i=0;i<input.size();i++)
{
k=k*10+(input[i]-‘0’);
k=k%1003;
}
return k;
}

void totalSum(TreeNode *root,string output,int &ans)
{
if(root==NULL)
{
return;
}

 if(root->left==NULL && root->right==NULL)
 {
     string s=output+to_string(root->val);
     ans+=getMod(s);
     return;
 }
 
 string s=to_string(root->val);
 totalSum(root->left,output+s,ans);
 totalSum(root->right,output+s,ans);

}

int Solution::sumNumbers(TreeNode* root) {
int ans=0;
totalSum(root,"",ans);
int result=ans%1003;
return result;
}