-
Notifications
You must be signed in to change notification settings - Fork 1
/
124_Binary_Tree_Maximum_Path_Sum.cpp
65 lines (51 loc) · 1.43 KB
/
124_Binary_Tree_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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// https://leetcode.com/problems/binary-tree-maximum-path-sum/description/
// https://leetcode.com/problems/binary-tree-maximum-path-sum/solutions/2902056/c-0-ms-approach-discussed-postorder-dfs-clean-code/
#include <iostream>
#include <algorithm>
using namespace std;
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) {}
};
int maxSum = -1e9;
int pathSum(TreeNode *root)
{
if (!root)
return 0;
int leftPathSum = pathSum(root->left);
int rightPathSum = pathSum(root->right);
int currSum = root->val + leftPathSum + rightPathSum;
maxSum = max({maxSum,
currSum,
root->val,
root->val + max(leftPathSum, rightPathSum)});
return max(
root->val,
root->val + max(leftPathSum, rightPathSum));
}
int maxPathSum(TreeNode *root)
{
return max(
pathSum(root),
maxSum);
}
int main()
{
// -10
// / \
// 9 20
// / \
// 15 7
TreeNode *root = new TreeNode(-10);
root->left = new TreeNode(9);
root->right = new TreeNode(20);
root->right->left = new TreeNode(15);
root->right->right = new TreeNode(7);
cout << maxPathSum(root) << "\n";
return 0;
}