Sum of Root To Leaf Binary Numbers Solution

'''

void preorder(TreeNode* r, int d,int *s)
{
if(r->left==NULL && r->right==NULL)
{
(s)=(s)+((d2)+(r->val));
return;
}
else{
d=(d
2)+(r->val);
if(r->left)
preorder(r->left,d,s);
if(r->right)
preorder(r->right,d,s);
}
return;
}

class Solution {
public:
int sumRootToLeaf(TreeNode* root) {
if(root==NULL)
return 0;
if(root->left==NULL and root->right==NULL)
return root->val;
int sum=0;
int decimalValue=0;
preorder(root,decimalValue,&sum);
return sum;
}
};

'''

Comments (0)