Given a binary tree and a number, return true if the tree has a root-to-leaf path such that adding up all the values along the path equals the given number. Return false if no such path can be found.
#include
using namespace std;
class node
{
public:
int data;
node* left;
node* right;
node(int d)
{
data=d;
left=NULL;
right=NULL;
}
};
node* build()
{
node* root=new node(4);
node* root_left=new node(1);
node* root_right=new node(2);
root->left=root_left;
root->right=root_right;
}
int has_path_sum(node* root,int sum)
{
if (root==NULL)
{
return 0;
}
int d1=root->data + has_path_sum(root->left,sum);
int d2=root->data + has_path_sum(root->right,sum);
if(d1==sum || d2==sum)
{
return 1;
}
else
{
return 0;
}
}
int main()
{
node* wow=build();
bool result=has_path_sum(wow,6);
cout<<result;
return 0;
}
what mistake i am commiting in this problem.