And also write a function such as
int hasPathSum(struct node* node,int sum)
where sum is the sum of nodes of any path like path1:27,path2:22
which returns true if sum is there in any path and false if not present.
I thought to write the routine as :
//binary tree
struct binarytree
{
int data;
binarytree *left;
binarytree *right;
};
//routine
int hasPathSum(struct node* node,int sum)
{
int count = 0;
while(node != NULL)
{
count += node->data;
node = node->left;
}
if (count == sum)
return 1;
else return 0;
}
But my problem is it is working only for path1 but is it possible to write a generalised way that works for all paths?
My Idea is to do the problem that works for any binary tree not just given above.
Thanks for your reply, I can't understand the above code since I am a beginner to c++ .could you explain it more briefly or give a more easily understandable solution.
within graphAux it takes indent, and pointer of the myroot, then it recursively calls the same function to go down the right side and left side while printing the data while increasing the indent by 8 for each recursion