forked from harshitbansal373/hacktoberfest2020
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Binary Tree Maximum Path Sum.cpp
38 lines (36 loc) · 1.11 KB
/
Binary Tree Maximum Path Sum.cpp
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
/*
Given a non-empty binary tree, find the maximum path sum.
For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path must contain at least one
node and does not need to go through the root.
*/
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int helper(TreeNode* root,int& ans)
{
if(!root)
return 0;
int l = helper(root->left,ans);
int r = helper(root->right,ans);
int tmp = max(root->val,max(l,r)+root->val);
ans = max(ans,max(tmp,l+root->val+r));
return tmp;
}
int maxPathSum(TreeNode* root) {
if(!root)
return 0;
int sum = INT_MIN;
helper(root,sum);
return sum;
}
};