-
Notifications
You must be signed in to change notification settings - Fork 118
/
124. Binary Tree Maximum Path Sum.cpp
37 lines (33 loc) · 1.24 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
//https://leetcode.com/problems/binary-tree-maximum-path-sum/discuss/39775/Accepted-short-solution-in-Java
//Runtime: 36 ms, faster than 33.76% of C++ online submissions for Binary Tree Maximum Path Sum.
//Memory Usage: 28.8 MB, less than 6.06% of C++ online submissions for Binary Tree Maximum Path Sum.
/**
* 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 ans;
int maxPathDown(TreeNode* node){
if(!node) return 0;
//max sum of path rooted at node->left, going down
int left = max(maxPathDown(node->left), 0);
//max sum of path rooted at node->right, going down
int right = max(maxPathDown(node->right), 0);
ans = max(ans, left+right+node->val);
//max sum of path that goes down
return node->val + max(left, right);
};
int maxPathSum(TreeNode* root) {
ans = INT_MIN;
maxPathDown(root);
return ans;
}
};