-
Notifications
You must be signed in to change notification settings - Fork 0
/
124_max_path_sum.cc
57 lines (49 loc) · 1.33 KB
/
124_max_path_sum.cc
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
52
53
54
55
56
57
#include <vector>
#include <iostream>
using namespace::std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
int postOrderTravel(TreeNode* node) {
if (node == NULL)
return INT32_MIN;
int lsum = postOrderTravel(node->left);
int rsum = postOrderTravel(node->right);
int nodesum;
int lvalue = node->left == NULL ? 0 : node->left->val;
int rvalue = node->right == NULL ? 0 : node->right->val;
int t = max(lvalue, rvalue);
int max1 = max(lsum, rsum);
if (lvalue <= 0 && rvalue <= 0)
nodesum = max(max1, node->val);
else if (lvalue <= 0)
nodesum = max(max1, node->val + rvalue);
else if (rvalue <= 0)
nodesum = max(max1, node->val + lvalue);
else
nodesum = max(max1, node->val + lvalue + rvalue);
if (t > 0)
node->val += t;
return nodesum;
}
int maxPathSum(TreeNode* root) {
return postOrderTravel(root);
}
};
int main()
{
Solution solu;
TreeNode* root = NULL;
unsigned n;
cin >> n;
while (n-- > 0) {
// create tree
}
cout << solu.maxPathSum(root) << endl;
return 0;
}