-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaxRootToLeaf.cpp
More file actions
executable file
·51 lines (41 loc) · 990 Bytes
/
maxRootToLeaf.cpp
File metadata and controls
executable file
·51 lines (41 loc) · 990 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include<stdio.h>
#include<limits.h>
/* A tree node structure */
struct node
{
int data;
struct node *left;
struct node *right;
};
struct node* newNode (int data)
{
struct node *temp = new struct node;
temp->data = data;
temp->left = NULL;
temp->right = NULL;
return temp;
}
int mx(int a,int b){
return a>b?a:b;
}
int maxSumPath(struct node *root){
if(root==NULL)
return 0;
return mx(maxSumPath(root->left),maxSumPath(root->right))+root->data;
}
int main()
{
struct node *root = NULL;
/* Constructing tree given in the above figure */
root = newNode(10);
root->left = newNode(-2);
root->right = newNode(-7);
root->right->left = newNode(11);
root->left->left = newNode(8);
root->left->right = newNode(14);
root->left->right->left=newNode(4);
int sum = maxSumPath(root);
printf ("\nMax Sum of the nodes is %d ", sum);
getchar();
return 0;
}