-
Notifications
You must be signed in to change notification settings - Fork 4
/
root_to_leaf_sum.C
50 lines (44 loc) · 1.03 KB
/
root_to_leaf_sum.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//author @ Ayush Aggarwal
//Root to leaf sum of a binary tree
#include <bits/stdc++.h>
using namespace std;
struct node
{
int data;
node *left,*right;
};
struct node* newnode(int data)
{
node *temp = new node;
temp->data = data;
temp->left = NULL;
temp->right = NULL;
return temp;
}
bool haspathsum(node *root, int sum)
{
if(root==NULL)
return false;;
if(root->left == NULL and root->right == NULL)
if(root->data == sum)
return true;
if(haspathsum(root->left,sum-root->data))
return true;
if(haspathsum(root->right,sum-root->data))
return true;
return false;
}
int main()
{
struct node *root = newnode(10);
root->left = newnode(8);
root->right = newnode(2);
root->left->left = newnode(3);
root->left->right = newnode(5);
root->right->left = newnode(2);
if(haspathsum(root, 14))
cout<<"Their exists a path from root to leaf in given tree";
else
cout<<"Their is no path exists in given tree";
return 0;
}