-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTreeMaximumPathSum.cpp
More file actions
35 lines (34 loc) · 1.09 KB
/
BinaryTreeMaximumPathSum.cpp
File metadata and controls
35 lines (34 loc) · 1.09 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
/**
* 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 maxPathSum(TreeNode* root) {
int ans = NEG_INF;
solve(root, ans);
return ans;
}
private:
const int NEG_INF = -4e7;
int solve(TreeNode* root, int& ans) {
if (!root) {
return NEG_INF;
}
int left = solve(root->left, ans);
int right = solve(root->right, ans);
// Take root and best path from the children, or make the end of the path root.
int best_continue = root->val+max({left, right, 0});
// Can also connect the best paths from the two children.
ans = max({ans, best_continue, root->val+left+right});
// Can only continue paths root to descendent or just root.
return best_continue;
}
};