Skip to content

Commit 135f0ca

Browse files
committed
updated
1 parent 7387052 commit 135f0ca

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

18-Binary Tree/max_sum_path.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include<iostream>
2+
using namespace std;
3+
class node {
4+
public:
5+
int data;
6+
node*left;
7+
node*right;
8+
node(int d){
9+
data=d;
10+
left=NULL;
11+
right= NULL;
12+
}
13+
};
14+
node *buildTree(){
15+
int d;cin>>d;
16+
if(d==-1){
17+
return NULL;
18+
}
19+
node*root= new node(d);
20+
root->left= buildTree();
21+
root->right= buildTree();
22+
return root;
23+
}
24+
void printIn(node*root){
25+
if(root==NULL){
26+
return;
27+
}
28+
cout<<root->data<<" ";
29+
printIn(root->left);
30+
printIn(root->right);
31+
}
32+
class Solution{
33+
public:
34+
int global_max=0;
35+
int maxSumPath(node* root){
36+
if(root==NULL){
37+
return 0;
38+
}
39+
int ls= maxSumPath(root->left);
40+
int rs= maxSumPath(root->right);
41+
int c1= root->data;
42+
int c2= ls+ root->data;
43+
int c3= rs+ root->data;
44+
int c4= ls+rs+root->data;
45+
global_max= max(c1,max(c2,max(c3,max(c4,global_max))));
46+
return max(c1,max(rs,0))+ root->data;
47+
}
48+
};
49+
50+
int main(int argc, char const *argv[])
51+
{
52+
node*root= buildTree();
53+
printIn(root);
54+
Solution s1;
55+
s1.maxSumPath(root);
56+
57+
}

0 commit comments

Comments
 (0)