-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary_tree_maximum_path_sum.cc
53 lines (48 loc) · 1.9 KB
/
binary_tree_maximum_path_sum.cc
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
// 写完后提交1次就AC了,太爽了,看来最朴实的分类讨论才是最稳当的玩法。
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int maxPathSum(TreeNode *root) {
// Return max_path_sum.
// In the end, root->val store the path_sum from some sub node to current node. In order to be used to add to parent node.
if (NULL == root) return 0;
if (NULL == root->left) {
if (NULL == root->right) {
return root->val;
} else {
int right_max_path_sum = maxPathSum(root->right);
if (root->right->val > 0) {
root->val += root->right->val;
}
return right_max_path_sum > root->val ? right_max_path_sum : root->val;
}
} else {
if (NULL == root->right) {
int left_max_path_sum = maxPathSum(root->left);
if (root->left->val > 0) {
root->val += root->left->val;
}
return left_max_path_sum > root->val ? left_max_path_sum : root->val;
} else {
int right_max_path_sum = maxPathSum(root->right);
int left_max_path_sum = maxPathSum(root->left);
int ret = root->val;
if (root->left->val > 0) ret += root->left->val;
if (root->right->val > 0) ret += root->right->val;
ret = max(ret, left_max_path_sum);
ret = max(ret, right_max_path_sum);
int sub_path_sum = max(root->left->val, root->right->val);
if (sub_path_sum > 0) root->val += sub_path_sum;
return ret;
}
}
}
};