-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram.cpp
More file actions
34 lines (33 loc) · 951 Bytes
/
program.cpp
File metadata and controls
34 lines (33 loc) · 951 Bytes
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
#include "../include/pre.h"
class Solution {
private:
template<typename T, typename ...Targs>
static T max(T t, Targs... args)
{
return max(t, max(args...));
}
template<typename T>
static T max(T t1, T t2)
{
return std::max(t1, t2);
}
std::unordered_map<TreeNode*, int> maxChain;
int maxValue;
int maxPathSum_helper(TreeNode* root)
{
if (root == NULL) return 0;
if (maxChain.find(root) != maxChain.end()) return maxChain[root];
auto l = maxPathSum_helper(root->left);
auto r = maxPathSum_helper(root->right);
maxValue = max(maxValue, root->val, max(l, r, l + r) + root->val);
maxChain[root] = root->val + std::max(0, std::max(l, r));
return maxChain[root];
}
public:
int maxPathSum(TreeNode* root)
{
maxValue = std::numeric_limits<int>::min();
maxPathSum_helper(root);
return maxValue;
}
};