Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c54388e

Browse files
committed
feat: add solutions to lc problem: No.0124
No.0124.Binary Tree Maximum Path Sum
1 parent d48d23c commit c54388e

File tree

4 files changed

+183
-13
lines changed

4 files changed

+183
-13
lines changed

‎solution/0100-0199/0124.Binary Tree Maximum Path Sum/README.md‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,73 @@ public:
192192
};
193193
```
194194
195+
### **JavaScript**
196+
197+
```js
198+
/**
199+
* Definition for a binary tree node.
200+
* function TreeNode(val, left, right) {
201+
* this.val = (val===undefined ? 0 : val)
202+
* this.left = (left===undefined ? null : left)
203+
* this.right = (right===undefined ? null : right)
204+
* }
205+
*/
206+
/**
207+
* @param {TreeNode} root
208+
* @return {number}
209+
*/
210+
var maxPathSum = function (root) {
211+
let ans = -1000;
212+
let dfs = function (root) {
213+
if (!root) {
214+
return 0;
215+
}
216+
const left = Math.max(0, dfs(root.left));
217+
const right = Math.max(0, dfs(root.right));
218+
ans = Math.max(ans, left + right + root.val);
219+
return root.val + Math.max(left, right);
220+
};
221+
dfs(root);
222+
return ans;
223+
};
224+
```
225+
226+
### **C#**
227+
228+
```cs
229+
/**
230+
* Definition for a binary tree node.
231+
* public class TreeNode {
232+
* public int val;
233+
* public TreeNode left;
234+
* public TreeNode right;
235+
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
236+
* this.val = val;
237+
* this.left = left;
238+
* this.right = right;
239+
* }
240+
* }
241+
*/
242+
public class Solution {
243+
private int ans;
244+
245+
public int MaxPathSum(TreeNode root) {
246+
ans = int.MinValue;
247+
dfs(root);
248+
return ans;
249+
}
250+
251+
private int dfs(TreeNode root)
252+
{
253+
if (root == null) return 0;
254+
int left = Math.Max(0, dfs(root.left));
255+
int right = Math.Max(0, dfs(root.right));
256+
ans = Math.Max(ans, left + right + root.val);
257+
return root.val + Math.Max(left, right);
258+
}
259+
}
260+
```
261+
195262
### **...**
196263

197264
```

‎solution/0100-0199/0124.Binary Tree Maximum Path Sum/README_EN.md‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,73 @@ public:
175175
};
176176
```
177177
178+
### **JavaScript**
179+
180+
```js
181+
/**
182+
* Definition for a binary tree node.
183+
* function TreeNode(val, left, right) {
184+
* this.val = (val===undefined ? 0 : val)
185+
* this.left = (left===undefined ? null : left)
186+
* this.right = (right===undefined ? null : right)
187+
* }
188+
*/
189+
/**
190+
* @param {TreeNode} root
191+
* @return {number}
192+
*/
193+
var maxPathSum = function (root) {
194+
let ans = -1000;
195+
let dfs = function (root) {
196+
if (!root) {
197+
return 0;
198+
}
199+
const left = Math.max(0, dfs(root.left));
200+
const right = Math.max(0, dfs(root.right));
201+
ans = Math.max(ans, left + right + root.val);
202+
return root.val + Math.max(left, right);
203+
};
204+
dfs(root);
205+
return ans;
206+
};
207+
```
208+
209+
### **C#**
210+
211+
```cs
212+
/**
213+
* Definition for a binary tree node.
214+
* public class TreeNode {
215+
* public int val;
216+
* public TreeNode left;
217+
* public TreeNode right;
218+
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
219+
* this.val = val;
220+
* this.left = left;
221+
* this.right = right;
222+
* }
223+
* }
224+
*/
225+
public class Solution {
226+
private int ans;
227+
228+
public int MaxPathSum(TreeNode root) {
229+
ans = int.MinValue;
230+
dfs(root);
231+
return ans;
232+
}
233+
234+
private int dfs(TreeNode root)
235+
{
236+
if (root == null) return 0;
237+
int left = Math.Max(0, dfs(root.left));
238+
int right = Math.Max(0, dfs(root.right));
239+
ans = Math.Max(ans, left + right + root.val);
240+
return root.val + Math.Max(left, right);
241+
}
242+
}
243+
```
244+
178245
### **...**
179246

180247
```
Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,31 @@
1-
using System;
2-
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* public int val;
5+
* public TreeNode left;
6+
* public TreeNode right;
7+
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
8+
* this.val = val;
9+
* this.left = left;
10+
* this.right = right;
11+
* }
12+
* }
13+
*/
314
public class Solution {
4-
private int _answer = int.MinValue;
15+
private int ans;
16+
517
public int MaxPathSum(TreeNode root) {
6-
GetMaxSumOfOneSide(root);
7-
return _answer;
18+
ans = int.MinValue;
19+
dfs(root);
20+
return ans;
821
}
922

10-
private int GetMaxSumOfOneSide(TreeNode root)
23+
private int dfs(TreeNode root)
1124
{
1225
if (root == null) return 0;
13-
var leftSum = GetMaxSumOfOneSide(root.left);
14-
if (leftSum < 0) leftSum = 0;
15-
var rightSum = GetMaxSumOfOneSide(root.right);
16-
if (rightSum < 0) rightSum = 0;
17-
var all = leftSum + rightSum + root.val;
18-
_answer = Math.Max(_answer, all);
19-
return Math.Max(root.val, root.val + Math.Max(leftSum, rightSum));
26+
int left = Math.Max(0, dfs(root.left));
27+
int right = Math.Max(0, dfs(root.right));
28+
ans = Math.Max(ans, left + right + root.val);
29+
return root.val + Math.Max(left, right);
2030
}
2131
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val, left, right) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.left = (left===undefined ? null : left)
6+
* this.right = (right===undefined ? null : right)
7+
* }
8+
*/
9+
/**
10+
* @param {TreeNode} root
11+
* @return {number}
12+
*/
13+
var maxPathSum = function (root) {
14+
let ans = -1000;
15+
let dfs = function (root) {
16+
if (!root) {
17+
return 0;
18+
}
19+
const left = Math.max(0, dfs(root.left));
20+
const right = Math.max(0, dfs(root.right));
21+
ans = Math.max(ans, left + right + root.val);
22+
return root.val + Math.max(left, right);
23+
};
24+
dfs(root);
25+
return ans;
26+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /