(- LeetCode)
Can someone please tell me where am I going wrong?
It returns negative value in some cases
long long int sum=0;
int binaryToDecimal(long long int n)
{
int num = n;
int dec_value = 0;
int base = 1;
int temp = num;
while (temp) {
int last_digit = temp % 10;
temp = temp / 10;
dec_value += last_digit * base;
base = base * 2;
}
return dec_value;
}
void fun(TreeNode *root,long long int v)
{
if(root==NULL)
return;
v=v*10;
v+=root->val;
if(root->left==NULL&&root->right==NULL)
{
sum=sum+binaryToDecimal(v);
return;
}
fun(root->left,v);
fun(root->right,v);
}
int sumRootToLeaf(TreeNode* root)
{
int v=0;
fun(root,0);
return sum;
}