42
42
43
43
<!-- 这里可写通用的实现逻辑 -->
44
44
45
- 思考二叉树递归问题的经典套路:
45
+ ** 方法一:递归**
46
+
47
+ 我们思考二叉树递归问题的经典套路:
46
48
47
49
1 . 终止条件(何时终止递归)
48
50
2 . 递归处理左右子树
49
51
3 . 合并左右子树的计算结果
50
52
51
- 对于本题,由于要满足题目对 “路径” 的定义,在返回当前子树对外贡献的最大路径和时,需要取 ` left ` ,` right ` 的最大值
53
+ 对于本题,我们设计一个函数 $dfs(root)$,它返回以 $root$ 为根节点的二叉树的最大路径和。
54
+
55
+ 函数 $dfs(root)$ 的执行逻辑如下:
56
+
57
+ 如果 $root$ 不存在,那么 $dfs(root)$ 返回 $0$;
58
+
59
+ 否则,我们递归计算 $root$ 的左子树和右子树的最大路径和,分别记为 $left$ 和 $right$。如果 $left$ 小于 $0$,那么我们将其置为 $0$,同理,如果 $right$ 小于 $0$,那么我们将其置为 $0$。
60
+
61
+ 然后,我们用 $root.val + left + right$ 更新答案。最后,函数返回 $root.val + \max(left, right)$。
62
+
63
+ 在主函数中,我们调用 $dfs(root)$,即可得到每个节点的最大路径和,其中的最大值即为答案。
64
+
65
+ 时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是二叉树的节点数。
52
66
53
67
<!-- tabs:start -->
54
68
64
78
# self.left = left
65
79
# self.right = right
66
80
class Solution :
67
- def maxPathSum (self , root : TreeNode) -> int :
68
- ans = - inf
69
-
70
- def dfs (node : TreeNode) -> int :
71
- if not node:
81
+ def maxPathSum (self , root : Optional[TreeNode]) -> int :
82
+ def dfs (root : Optional[TreeNode]) -> int :
83
+ if root is None :
72
84
return 0
73
- left = max (0 , dfs(node .left))
74
- right = max (0 , dfs(node .right))
85
+ left = max (0 , dfs(root .left))
86
+ right = max (0 , dfs(root .right))
75
87
nonlocal ans
76
- ans = max (ans, node .val + left + right)
77
- return node .val + max (left, right)
88
+ ans = max (ans, root .val + left + right)
89
+ return root .val + max (left, right)
78
90
91
+ ans = - inf
79
92
dfs(root)
80
93
return ans
81
94
```
@@ -101,25 +114,58 @@ class Solution:
101
114
* }
102
115
*/
103
116
class Solution {
104
- private int ans = Integer . MIN_VALUE ;
117
+ private int ans = - 1001 ;
105
118
106
119
public int maxPathSum (TreeNode root ) {
107
120
dfs(root);
108
121
return ans;
109
122
}
110
123
111
- private int dfs (TreeNode node ) {
112
- if (node == null ) {
124
+ private int dfs (TreeNode root ) {
125
+ if (root == null ) {
113
126
return 0 ;
114
127
}
115
- int left = Math . max(0 , dfs(node . left));
116
- int right = Math . max(0 , dfs(node . right));
117
- ans = Math . max(ans, node . val + left + right);
118
- return node . val + Math . max(left, right);
128
+ int left = Math . max(0 , dfs(root . left));
129
+ int right = Math . max(0 , dfs(root . right));
130
+ ans = Math . max(ans, root . val + left + right);
131
+ return root . val + Math . max(left, right);
119
132
}
120
133
}
121
134
```
122
135
136
+ ### ** C++**
137
+
138
+ ``` cpp
139
+ /* *
140
+ * Definition for a binary tree node.
141
+ * struct TreeNode {
142
+ * int val;
143
+ * TreeNode *left;
144
+ * TreeNode *right;
145
+ * TreeNode() : val(0), left(nullptr), right(nullptr) {}
146
+ * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
147
+ * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
148
+ * };
149
+ */
150
+ class Solution {
151
+ public:
152
+ int maxPathSum(TreeNode* root) {
153
+ int ans = -1001;
154
+ function<int(TreeNode* )> dfs = [ &] (TreeNode* root) {
155
+ if (!root) {
156
+ return 0;
157
+ }
158
+ int left = max(0, dfs(root->left));
159
+ int right = max(0, dfs(root->right));
160
+ ans = max(ans, left + right + root->val);
161
+ return root->val + max(left, right);
162
+ };
163
+ dfs(root);
164
+ return ans;
165
+ }
166
+ };
167
+ ```
168
+
123
169
### **Go**
124
170
125
171
```go
@@ -132,19 +178,17 @@ class Solution {
132
178
* }
133
179
*/
134
180
func maxPathSum(root *TreeNode) int {
135
- ans := math.MinInt32
136
-
181
+ ans := -1001
137
182
var dfs func(*TreeNode) int
138
- dfs = func (node *TreeNode) int {
139
- if node == nil {
183
+ dfs = func(root *TreeNode) int {
184
+ if root == nil {
140
185
return 0
141
186
}
142
- left := max (0 , dfs (node .Left ))
143
- right := max (0 , dfs (node .Right ))
144
- ans = max (ans, node. Val + left+right)
145
- return node. Val + max (left, right)
187
+ left := max(0, dfs(root .Left))
188
+ right := max(0, dfs(root .Right))
189
+ ans = max(ans, left+right+root.Val )
190
+ return max(left, right) + root.Val
146
191
}
147
-
148
192
dfs(root)
149
193
return ans
150
194
}
@@ -157,39 +201,37 @@ func max(a, b int) int {
157
201
}
158
202
```
159
203
160
- ### ** C++ **
204
+ ### ** TypeScript **
161
205
162
- ``` cpp
206
+ ``` ts
163
207
/**
164
208
* Definition for a binary tree node.
165
- * struct TreeNode {
166
- * int val;
167
- * TreeNode *left;
168
- * TreeNode *right;
169
- * TreeNode() : val(0), left(nullptr), right(nullptr) {}
170
- * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
171
- * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
172
- * };
209
+ * class TreeNode {
210
+ * val: number
211
+ * left: TreeNode | null
212
+ * right: TreeNode | null
213
+ * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
214
+ * this.val = (val===undefined ? 0 : val)
215
+ * this.left = (left===undefined ? null : left)
216
+ * this.right = (right===undefined ? null : right)
217
+ * }
218
+ * }
173
219
*/
174
- class Solution {
175
- public:
176
- int maxPathSum(TreeNode* root) {
177
- int ans = INT_MIN;
178
220
179
- function<int(TreeNode*)> dfs = [&]( TreeNode* node) {
180
- if (node == nullptr) {
181
- return 0;
182
- }
183
- int left = max( 0 , dfs(node->left)) ;
184
- int right = max( 0 , dfs(node->right));
185
- ans = max(ans, node->val + left + right );
186
- return node->val + max(left, right);
187
- } ;
188
-
189
- dfs (root) ;
190
- return ans ;
191
- }
192
- };
221
+ function maxPathSum( root : TreeNode | null ) : number {
222
+ let ans = - 1001 ;
223
+ const dfs = ( root : TreeNode | null ) : number => {
224
+ if ( ! root ) {
225
+ return 0 ;
226
+ }
227
+ const left = Math . max (0 , dfs ( root . left ) );
228
+ const right = Math . max (0 , dfs ( root . right ) );
229
+ ans = Math . max ( ans , left + right + root . val ) ;
230
+ return Math . max ( left , right ) + root . val ;
231
+ } ;
232
+ dfs ( root ) ;
233
+ return ans ;
234
+ }
193
235
```
194
236
195
237
### ** JavaScript**
@@ -208,15 +250,15 @@ public:
208
250
* @return {number}
209
251
*/
210
252
var maxPathSum = function (root ) {
211
- let ans = -1000 ;
212
- let dfs = function ( root) {
253
+ let ans = - 1001 ;
254
+ const dfs = root => {
213
255
if (! root) {
214
256
return 0 ;
215
257
}
216
258
const left = Math .max (0 , dfs (root .left ));
217
259
const right = Math .max (0 , dfs (root .right ));
218
260
ans = Math .max (ans, left + right + root .val );
219
- return root.val + Math.max(left, right);
261
+ return Math .max (left, right) + root . val ;
220
262
};
221
263
dfs (root);
222
264
return ans;
@@ -240,17 +282,17 @@ var maxPathSum = function (root) {
240
282
* }
241
283
*/
242
284
public class Solution {
243
- private int ans ;
285
+ private int ans = - 1001 ;
244
286
245
287
public int MaxPathSum (TreeNode root ) {
246
- ans = int .MinValue ;
247
288
dfs (root );
248
289
return ans ;
249
290
}
250
291
251
- private int dfs (TreeNode root )
252
- {
253
- if (root == null ) return 0 ;
292
+ private int dfs (TreeNode root ) {
293
+ if (root == null ) {
294
+ return 0 ;
295
+ }
254
296
int left = Math .Max (0 , dfs (root .left ));
255
297
int right = Math .Max (0 , dfs (root .right ));
256
298
ans = Math .Max (ans , left + right + root .val );
0 commit comments