forked from ByteByteGoHq/coding-interview-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaximum_path_sum.cpp
37 lines (34 loc) · 1.19 KB
/
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
#include <algorithm>
#include <limits>
#include "ds/TreeNode.h"
using ds::TreeNode;
/**
* Definition of TreeNode:
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
int maxPathSum(TreeNode* root) {
int maxSum = std::numeric_limits<int>::min();
maxPathSumHelper(root, maxSum);
return maxSum;
}
int maxPathSumHelper(TreeNode* node, int& maxSum) {
// Base case: null nodes have no path sum.
if (!node)
return 0;
// Collect the maximum gain we can attain from the left and right
// subtrees, setting them to 0 if they're negative.
int leftSum = std::max(maxPathSumHelper(node->left, maxSum), 0);
int rightSum = std::max(maxPathSumHelper(node->right, maxSum), 0);
// Update the overall maximum path sum if the current path sum is
// larger.
maxSum = std::max(maxSum, node->val + leftSum + rightSum);
// Return the maximum sum of a single, continuous path with the
// current node as an endpoint.
return node->val + std::max(leftSum, rightSum);
}