48
48
49
49
<!-- 这里可写通用的实现逻辑 -->
50
50
51
- 思考二叉树递归问题的经典套路:
51
+ ** 方法一:递归**
52
+
53
+ 我们思考二叉树递归问题的经典套路:
52
54
53
55
1 . 终止条件(何时终止递归)
54
56
2 . 递归处理左右子树
55
57
3 . 合并左右子树的计算结果
56
58
57
- 对于本题,由于要满足题目对“路径”的定义,在返回当前子树对外贡献的最大路径和时,需要取 ` left ` ,` right ` 的最大值
59
+ 对于本题,我们设计一个函数 $dfs(root)$,它返回以 $root$ 为根节点的二叉树的最大路径和。
60
+
61
+ 函数 $dfs(root)$ 的执行逻辑如下:
62
+
63
+ 如果 $root$ 不存在,那么 $dfs(root)$ 返回 $0$;
64
+
65
+ 否则,我们递归计算 $root$ 的左子树和右子树的最大路径和,分别记为 $left$ 和 $right$。如果 $left$ 小于 $0$,那么我们将其置为 $0$,同理,如果 $right$ 小于 $0$,那么我们将其置为 $0$。
66
+
67
+ 然后,我们用 $root.val + left + right$ 更新答案。最后,函数返回 $root.val + \max(left, right)$。
68
+
69
+ 在主函数中,我们调用 $dfs(root)$,即可得到每个节点的最大路径和,其中的最大值即为答案。
70
+
71
+ 时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是二叉树的节点数。
58
72
59
73
<!-- tabs:start -->
60
74
70
84
# self.left = left
71
85
# self.right = right
72
86
class Solution :
73
- def maxPathSum (self , root : TreeNode) -> int :
74
- ans = - inf
75
-
76
- def dfs (node : TreeNode) -> int :
77
- if not node:
87
+ def maxPathSum (self , root : Optional[TreeNode]) -> int :
88
+ def dfs (root : Optional[TreeNode]) -> int :
89
+ if root is None :
78
90
return 0
79
- left = max (0 , dfs(node .left))
80
- right = max (0 , dfs(node .right))
91
+ left = max (0 , dfs(root .left))
92
+ right = max (0 , dfs(root .right))
81
93
nonlocal ans
82
- ans = max (ans, node .val + left + right)
83
- return node .val + max (left, right)
94
+ ans = max (ans, root .val + left + right)
95
+ return root .val + max (left, right)
84
96
97
+ ans = - inf
85
98
dfs(root)
86
99
return ans
87
100
```
@@ -107,25 +120,58 @@ class Solution:
107
120
* }
108
121
*/
109
122
class Solution {
110
- private int ans = Integer . MIN_VALUE ;
123
+ private int ans = - 1001 ;
111
124
112
125
public int maxPathSum (TreeNode root ) {
113
126
dfs(root);
114
127
return ans;
115
128
}
116
129
117
- private int dfs (TreeNode node ) {
118
- if (node == null ) {
130
+ private int dfs (TreeNode root ) {
131
+ if (root == null ) {
119
132
return 0 ;
120
133
}
121
- int left = Math . max(0 , dfs(node . left));
122
- int right = Math . max(0 , dfs(node . right));
123
- ans = Math . max(ans, node . val + left + right);
124
- return node . val + Math . max(left, right);
134
+ int left = Math . max(0 , dfs(root . left));
135
+ int right = Math . max(0 , dfs(root . right));
136
+ ans = Math . max(ans, root . val + left + right);
137
+ return root . val + Math . max(left, right);
125
138
}
126
139
}
127
140
```
128
141
142
+ ### ** C++**
143
+
144
+ ``` cpp
145
+ /* *
146
+ * Definition for a binary tree node.
147
+ * struct TreeNode {
148
+ * int val;
149
+ * TreeNode *left;
150
+ * TreeNode *right;
151
+ * TreeNode() : val(0), left(nullptr), right(nullptr) {}
152
+ * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
153
+ * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
154
+ * };
155
+ */
156
+ class Solution {
157
+ public:
158
+ int maxPathSum(TreeNode* root) {
159
+ int ans = -1001;
160
+ function<int(TreeNode* )> dfs = [ &] (TreeNode* root) {
161
+ if (!root) {
162
+ return 0;
163
+ }
164
+ int left = max(0, dfs(root->left));
165
+ int right = max(0, dfs(root->right));
166
+ ans = max(ans, left + right + root->val);
167
+ return root->val + max(left, right);
168
+ };
169
+ dfs(root);
170
+ return ans;
171
+ }
172
+ };
173
+ ```
174
+
129
175
### **Go**
130
176
131
177
```go
@@ -138,19 +184,17 @@ class Solution {
138
184
* }
139
185
*/
140
186
func maxPathSum(root *TreeNode) int {
141
- ans := math.MinInt32
142
-
187
+ ans := -1001
143
188
var dfs func(*TreeNode) int
144
- dfs = func (node *TreeNode) int {
145
- if node == nil {
189
+ dfs = func(root *TreeNode) int {
190
+ if root == nil {
146
191
return 0
147
192
}
148
- left := max (0 , dfs (node .Left ))
149
- right := max (0 , dfs (node .Right ))
150
- ans = max (ans, node. Val + left+right)
151
- return node. Val + max (left, right)
193
+ left := max(0, dfs(root .Left))
194
+ right := max(0, dfs(root .Right))
195
+ ans = max(ans, left+right+root.Val )
196
+ return max(left, right) + root.Val
152
197
}
153
-
154
198
dfs(root)
155
199
return ans
156
200
}
@@ -163,39 +207,147 @@ func max(a, b int) int {
163
207
}
164
208
```
165
209
166
- ### ** C++ **
210
+ ### ** TypeScript **
167
211
168
- ``` cpp
212
+ ``` ts
169
213
/**
170
214
* Definition for a binary tree node.
171
- * struct TreeNode {
172
- * int val;
173
- * TreeNode *left;
174
- * TreeNode *right;
175
- * TreeNode() : val(0), left(nullptr), right(nullptr) {}
176
- * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
177
- * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
178
- * };
215
+ * class TreeNode {
216
+ * val: number
217
+ * left: TreeNode | null
218
+ * right: TreeNode | null
219
+ * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
220
+ * this.val = (val===undefined ? 0 : val)
221
+ * this.left = (left===undefined ? null : left)
222
+ * this.right = (right===undefined ? null : right)
223
+ * }
224
+ * }
179
225
*/
180
- class Solution {
181
- public:
182
- int maxPathSum(TreeNode* root) {
183
- int ans = INT_MIN;
184
226
185
- function<int(TreeNode*)> dfs = [&](TreeNode* node) {
186
- if (node == nullptr) {
187
- return 0;
188
- }
189
- int left = max(0 , dfs(node->left));
190
- int right = max(0 , dfs(node->right));
191
- ans = max(ans, node->val + left + right);
192
- return node->val + max(left, right);
193
- };
227
+ function maxPathSum(root : TreeNode | null ): number {
228
+ let ans = - 1001 ;
229
+ const dfs = (root : TreeNode | null ): number => {
230
+ if (! root ) {
231
+ return 0 ;
232
+ }
233
+ const left = Math .max (0 , dfs (root .left ));
234
+ const right = Math .max (0 , dfs (root .right ));
235
+ ans = Math .max (ans , left + right + root .val );
236
+ return Math .max (left , right ) + root .val ;
237
+ };
238
+ dfs (root );
239
+ return ans ;
240
+ }
241
+ ```
242
+
243
+ ### ** JavaScript**
244
+
245
+ ``` js
246
+ /**
247
+ * Definition for a binary tree node.
248
+ * function TreeNode(val, left, right) {
249
+ * this.val = (val===undefined ? 0 : val)
250
+ * this.left = (left===undefined ? null : left)
251
+ * this.right = (right===undefined ? null : right)
252
+ * }
253
+ */
254
+ /**
255
+ * @param {TreeNode} root
256
+ * @return {number}
257
+ */
258
+ var maxPathSum = function (root ) {
259
+ let ans = - 1001 ;
260
+ const dfs = root => {
261
+ if (! root) {
262
+ return 0 ;
263
+ }
264
+ const left = Math .max (0 , dfs (root .left ));
265
+ const right = Math .max (0 , dfs (root .right ));
266
+ ans = Math .max (ans, left + right + root .val );
267
+ return Math .max (left, right) + root .val ;
268
+ };
269
+ dfs (root);
270
+ return ans;
271
+ };
272
+ ```
273
+
274
+ ### ** C#**
194
275
276
+ ``` cs
277
+ /**
278
+ * Definition for a binary tree node.
279
+ * public class TreeNode {
280
+ * public int val;
281
+ * public TreeNode left;
282
+ * public TreeNode right;
283
+ * public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
284
+ * this.val = val;
285
+ * this.left = left;
286
+ * this.right = right;
287
+ * }
288
+ * }
289
+ */
290
+ public class Solution {
291
+ private int ans = - 1001 ;
292
+
293
+ public int MaxPathSum (TreeNode root ) {
195
294
dfs (root );
196
295
return ans ;
197
296
}
198
- };
297
+
298
+ private int dfs (TreeNode root ) {
299
+ if (root == null ) {
300
+ return 0 ;
301
+ }
302
+ int left = Math .Max (0 , dfs (root .left ));
303
+ int right = Math .Max (0 , dfs (root .right ));
304
+ ans = Math .Max (ans , left + right + root .val );
305
+ return root .val + Math .Max (left , right );
306
+ }
307
+ }
308
+ ```
309
+
310
+ ### ** Rust**
311
+
312
+ ``` rust
313
+ // Definition for a binary tree node.
314
+ // #[derive(Debug, PartialEq, Eq)]
315
+ // pub struct TreeNode {
316
+ // pub val: i32,
317
+ // pub left: Option<Rc<RefCell<TreeNode>>>,
318
+ // pub right: Option<Rc<RefCell<TreeNode>>>,
319
+ // }
320
+ //
321
+ // impl TreeNode {
322
+ // #[inline]
323
+ // pub fn new(val: i32) -> Self {
324
+ // TreeNode {
325
+ // val,
326
+ // left: None,
327
+ // right: None
328
+ // }
329
+ // }
330
+ // }
331
+ use std :: rc :: Rc ;
332
+ use std :: cell :: RefCell ;
333
+ impl Solution {
334
+ fn dfs (root : & Option <Rc <RefCell <TreeNode >>>, res : & mut i32 ) -> i32 {
335
+ if root . is_none () {
336
+ return 0 ;
337
+ }
338
+ let node = root . as_ref (). unwrap (). borrow ();
339
+ let left = 0. max (Self :: dfs (& node . left, res ));
340
+ let right = 0. max (Self :: dfs (& node . right, res ));
341
+ * res = (node . val + left + right ). max (* res );
342
+ node . val + left . max (right )
343
+ }
344
+
345
+ pub fn max_path_sum (root : Option <Rc <RefCell <TreeNode >>>) -> i32 {
346
+ let mut res = - 1000 ;
347
+ Self :: dfs (& root , & mut res );
348
+ res
349
+ }
350
+ }
199
351
```
200
352
201
353
### ** ...**
0 commit comments