-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsum_root_leaf.c
More file actions
37 lines (30 loc) · 821 Bytes
/
Copy pathsum_root_leaf.c
File metadata and controls
37 lines (30 loc) · 821 Bytes
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
/* Sum of root to leaf equal to input value
For eg, input 19, the nodes from root 1,5,6,7 add upto 19
1
2 5
4 3 NL 6
NL NL 8 7 */
#include <stdio.h>
typedef struct node_ {
int data;
struct node_ *left, *right;
} node;
void preorder (node *root, int *sum, int value)
{
if (!root)
return;
*sum += root->data;
if ((!root->left && !root->right) && (*sum == value))
printf("Found leaf %d \n", root->data);
preorder(root->left, sum, value);
preorder(root->right, sum, value);
*sum -= root->data;
}
int main()
{
int sum = 0;
node n8 = {8, NULL, NULL}, n7 = {7, NULL, NULL}, n6 = {6, &n8, &n7}, n5 = {5, NULL, &n6};
node n4 = {4, NULL, NULL}, n3 = {3, NULL, NULL}, n2 = {2, &n4, &n3}, n1 = {1, &n2, &n5};
preorder(&n1, &sum, 19);
return 0;
}