Skip to content

Commit 81c1108

Browse files
authoredOct 10, 2022
Create BinaryTree MaximumPathSum
1 parent e66795f commit 81c1108

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed
 

‎BinaryTree MaximumPathSum

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
int solve(TreeNode* root,int& mx){
2+
if(!root) return 0;
3+
4+
int left=max(0,solve(root->left,mx));
5+
int right=max(0,solve(root->right,mx));
6+
int currVal=root->val;
7+
8+
mx=max(mx,left+right+currVal);
9+
return max(left,right)+currVal;
10+
}
11+
int maxPathSum(TreeNode* root) {
12+
int mx=INT_MIN;
13+
solve(root,mx);
14+
return mx;
15+
}

0 commit comments

Comments
 (0)
Please sign in to comment.