-
Notifications
You must be signed in to change notification settings - Fork 0
/
200208-1.cpp
109 lines (105 loc) · 2.38 KB
/
200208-1.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// https://leetcode-cn.com/problems/binary-tree-maximum-path-sum/
#include <cstdio>
#include <algorithm>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
void release(TreeNode* t)
{
if (t) {
release(t->left);
release(t->right);
delete t;
}
}
class Solution {
public:
int maxPathSum(TreeNode* root) {
if (!root->left && !root->right) return root->val;
int a = maxRootPathSum(root->left);
int b = maxRootPathSum(root->right);
int s = a + root->val + b;
if (s < root->val + a) { s = root->val + a; }
if (s < root->val + b) { s = root->val + b; }
if (s < root->val) { s = root->val; }
if (root->left && s < a) { s = a; }
if (root->right && s < b) { s = b; }
if (root->left) {
a = maxPathSum(root->left);
if (s < a) { s = a; }
}
if (root->right) {
b = maxPathSum(root->right);
if (s < b) { s = b; }
}
return s;
}
private:
int maxRootPathSum(TreeNode* t) { // path that starts with root node
if (!t) return 0;
int a = maxRootPathSum(t->left);
int b = maxRootPathSum(t->right);
return t->val + max(0, max(a, b));
}
};
int main()
{
Solution s;
{
TreeNode* t = new TreeNode(1);
t->left = new TreeNode(2);
t->right = new TreeNode(3);
printf("%d\n", s.maxPathSum(t)); // answer: 6
release(t);
}
{
TreeNode* t = new TreeNode(-10);
t->left = new TreeNode(9);
t->right = new TreeNode(20);
t->right->left = new TreeNode(15);
t->right->right = new TreeNode(7);
printf("%d\n", s.maxPathSum(t)); // answer: 42
release(t);
}
{
TreeNode* t = new TreeNode(-3);
printf("%d\n", s.maxPathSum(t)); // answer: -3
release(t);
}
{
TreeNode* t = new TreeNode(-2);
t->right = new TreeNode(-1);
printf("%d\n", s.maxPathSum(t)); // answer: -1
release(t);
}
{
TreeNode* t = new TreeNode(9);
t->left = new TreeNode(6);
t->right = new TreeNode(-3);
t->right->left = new TreeNode(-6);
t->right->right = new TreeNode(2);
t->right->right->left = new TreeNode(2);
t->right->right->left->left = new TreeNode(-6);
t->right->right->left->right = new TreeNode(-6);
t->right->right->left->left->left = new TreeNode(-6);
/* 9
* / \
* 6 -3
* / \
* -6 2
* /
* 2
* / \
* -6 -6
* /
* -6
*/
printf("%d\n", s.maxPathSum(t)); // answer: 16
release(t);
}
return 0;
}