I know how to find if a binary tree has a certain path with a given sum (If this is not the best way please let me know):
int pathSum(MyNode root, int sum)
{
if(root == null)
return -1;
int temp = sum - root.value;
return(pathSum(root.left,temp) || pathSum(root.right,temp));
}
What I am not able to figure out is how to print the particular path.
My Node class looks like this:
class MyNode {
int value;
MyNode left;
MyNode right;
MyNode(int value)
{
this.value = value;
}
}
Try this, use overloading:
This code is tested with the following classes:
Result: