-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinary_Tree_Maximum_Path_Sum
More file actions
46 lines (37 loc) · 1.22 KB
/
Binary_Tree_Maximum_Path_Sum
File metadata and controls
46 lines (37 loc) · 1.22 KB
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
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
pair<int, int> makePair(int first, int second){
return pair<int, int>(first,second);
}
int biggest3(int a, int b, int c){
if (a>b)
if (a>c) return a;
else return c;
else if (b>c) return b;
else return c;
}
pair<int, int> pathSum(TreeNode *root){
if (root==NULL) return makePair(0,INT_MIN);
pair<int, int> left = pathSum(root->left);
pair<int, int> right = pathSum(root->right);
int max1 = biggest3(left.first , right.first, 0) + root->val;
int max2 = (left.first>0?left.first:0) + (right.first>0?right.first:0) + root->val;
if (left.second > max2) max2 = left.second;
if (right.second > max2) max2 = right.second;
return makePair(max1, max2);
}
int maxPathSum(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
return pathSum(root).second;
}
};