Hello to everyone..

I have made a function in order to add the values of the nodes on each path of the tree. For example if I enter numbers 7, 8, 9, 4, 1 then for the path (7>4>1) the cost must be 7+4+1=12.

The maximum cost is wanted.

I give you the code of the function. I have a problem because despite the fact it adds correctly for the traversal to the left, it doesn't seem to "remember" correctly the maxCost value when changing traversal. In addition, it seems to return in the end the root value as maxCost.

Could you help me??

Code:

int findcosthelp (node n, int cost, int maxCost)
{
if (n != NULL) {
cost=cost + n->key;
printf("%d %d ", cost, maxCost);
if (cost > maxCost)
maxCost = cost;
findcosthelp(n->left, cost, maxCost);
findcosthelp(n->right, cost, maxCost);
}
return cost;
}
int findcost (node n, int cost, int maxCost)
{
cost=0;
maxcost=0;
findcosthelp(n, cost, maxcost);
}