-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaxPathSumInBinaryTree.h
111 lines (102 loc) · 2.69 KB
/
maxPathSumInBinaryTree.h
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
110
111
//
// Created by yindong on 19-6-15.
//
#ifndef SRC_MAXPATHSUMINBINARYTREE_H
#define SRC_MAXPATHSUMINBINARYTREE_H
#include<algorithm>
#include<queue>
using namespace std;
// Definition for a binary tree node.
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
TreeNode *buildTree(vector<string> vec){
/* vec 中至少有一个数
* # 代表空指针
* i 记录 在vec中 的位置,队列 q 记录尚未添加子树的节点
* 重复:
* i < size of vector:
* 将当前位置 i 非空,链接到队首节点的左子树,节点入队
* i++
* 如果 i < sizeof vector , 将当前数字非空, 链接到队首节点右子树,节点入队
* i++
*
* 返回根节点
*/
queue<TreeNode *> q;
TreeNode *root = new TreeNode(stoi(vec[0]));
q.push(root);
int i = 1;
TreeNode *newNode, *curNode;
int tmp_val;
while( i < vec.size()){
curNode = q.front();
q.pop();
if(vec[i] != "#"){
tmp_val = stoi(vec[i]);
newNode = new TreeNode(tmp_val);
curNode->left = newNode;
q.push(newNode);
}
i++;
if( i < vec.size()){
if(vec[i] != "#"){
tmp_val = stoi(vec[i]);
newNode = new TreeNode(tmp_val);
curNode->right = newNode;
q.push(newNode);
}
i++;
}
}
return root;
}
struct Record{
int R;
int q;
};
class MaxPathSumInBinaryTree {
public:
int maxPathSum(TreeNode* root) {
auto res = maxSubTree(root);
return res.R;
}
Record maxSubTree(TreeNode* root){
// 当遇到叶节点, R = q = left->value
if(not root->left and not root->right){
return Record{root->val, root->val};
}
// 非叶节点,注意左右子数有一个为空的情况
Record left, right;
if(root->left){
left = maxSubTree(root->left);
}
if(root->right){
right = maxSubTree(root->right);
}
int q = root->val;
if(root->left){
q = max(root->val + left.q, q);
}
if(root->right){
q = max(root->val + right.q, q);
}
int s = q;
if(root->left and root->right){
s = max(q, root->val + right.q + left.q);
}
int R = s;
if(root->left){
R = max(R, left.R);
}
if(root->right){
R = max(R, right.R);
}
cout << R << ' ' << q << endl;
return Record{R, q};
}
};
#endif //SRC_MAXPATHSUMINBINARYTREE_H