-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBT_Max_PathSum.cpp
98 lines (69 loc) · 1.95 KB
/
BT_Max_PathSum.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
#include <climits>
#include <iostream>
#include <cmath>
class TreeNode {
public:
int val;
TreeNode *left, *right;
TreeNode(int val) {
this->val = val;
this->left = this->right = NULL;
}
};
/*
* Binary Tree Max Path Sum
思路:
计算树的最长path有2种情况:
1. 通过根的path.
(1)如果左子树从左树根到任何一个Node的path大于零,可以链到root上
(2)如果右子树从右树根到任何一个Node的path大于零,可以链到root上
2. 不通过根的path. 这个可以取左子树及右子树的path的最大值。
所以创建一个inner class:
记录2个值:
1. 本树的最大path。
2. 本树从根节点出发到任何一个节点的最大path.
注意,当root == null,以上2个值都要置为Integer_MIN_VALUE; 因为没有节点可取的时候,是不存在solution的。以免干扰递归的计算
*/
class Solution {
public:
/**
* @param root: The root of binary tree.
* @return: An integer
*/
class ReturnType{
public:
int maxSingle;
int max;
ReturnType(int maxSingle, int max){
this->max = max;
this->maxSingle = maxSingle;
}
};
int maxPathSum(TreeNode *root){
return dfs(root).max;
}
//深搜
ReturnType dfs(TreeNode *root){
ReturnType ret(INT_MIN, INT_MIN);
if(root == NULL) return ret;
//divide
ReturnType left = dfs(root->left);
ReturnType right = dfs(root->right);
int crossRoot = root->val;
//conquer
//if any of the path of left and right is < 0, do not add it.
crossRoot +=fmax(0, left.maxSingle);
crossRoot +=fmax(0, right.maxSingle);
int maxSingle = fmax(left.maxSingle, right.maxSingle);
maxSingle = fmax(maxSingle, 0);
maxSingle += root->val;
ret.maxSingle = maxSingle;
ret.max = fmax(right.max, left.max);
ret.max = fmax(ret.max, crossRoot);
return ret;
}
};
int main(){
// do sth
return 0;
}