Skip to content

Commit a732ce3

Browse files
committedJun 20, 2023
feat: add solutions to lc problem: No.0124
No.0124.Binary Tree Maximum Path Sum
1 parent e920101 commit a732ce3

File tree

12 files changed

+368
-259
lines changed

12 files changed

+368
-259
lines changed
 

‎solution/0000-0099/0045.Jump Game II/README.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,7 @@ func max(a, b int) int {
149149

150150
```ts
151151
function jump(nums: number[]): number {
152-
let ans = 0;
153-
let mx = 0;
154-
let last = 0;
152+
let [ans, mx, last] = [0, 0, 0];
155153
for (let i = 0; i < nums.length - 1; ++i) {
156154
mx = Math.max(mx, i + nums[i]);
157155
if (last === i) {

‎solution/0000-0099/0045.Jump Game II/README_EN.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,7 @@ func max(a, b int) int {
132132

133133
```ts
134134
function jump(nums: number[]): number {
135-
let ans = 0;
136-
let mx = 0;
137-
let last = 0;
135+
let [ans, mx, last] = [0, 0, 0];
138136
for (let i = 0; i < nums.length - 1; ++i) {
139137
mx = Math.max(mx, i + nums[i]);
140138
if (last === i) {

‎solution/0000-0099/0045.Jump Game II/Solution.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
function jump(nums: number[]): number {
2-
let ans = 0;
3-
let mx = 0;
4-
let last = 0;
2+
let [ans, mx, last] = [0, 0, 0];
53
for (let i = 0; i < nums.length - 1; ++i) {
64
mx = Math.max(mx, i + nums[i]);
75
if (last === i) {

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

+105-63
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,27 @@
4242

4343
<!-- 这里可写通用的实现逻辑 -->
4444

45-
思考二叉树递归问题的经典套路:
45+
**方法一:递归**
46+
47+
我们思考二叉树递归问题的经典套路:
4648

4749
1. 终止条件(何时终止递归)
4850
2. 递归处理左右子树
4951
3. 合并左右子树的计算结果
5052

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$ 是二叉树的节点数。
5266

5367
<!-- tabs:start -->
5468

@@ -64,18 +78,17 @@
6478
# self.left = left
6579
# self.right = right
6680
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:
7284
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))
7587
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)
7890

91+
ans = -inf
7992
dfs(root)
8093
return ans
8194
```
@@ -101,25 +114,58 @@ class Solution:
101114
* }
102115
*/
103116
class Solution {
104-
private int ans = Integer.MIN_VALUE;
117+
private int ans = -1001;
105118

106119
public int maxPathSum(TreeNode root) {
107120
dfs(root);
108121
return ans;
109122
}
110123

111-
private int dfs(TreeNode node) {
112-
if (node == null) {
124+
private int dfs(TreeNode root) {
125+
if (root == null) {
113126
return 0;
114127
}
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);
119132
}
120133
}
121134
```
122135

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+
123169
### **Go**
124170
125171
```go
@@ -132,19 +178,17 @@ class Solution {
132178
* }
133179
*/
134180
func maxPathSum(root *TreeNode) int {
135-
ans := math.MinInt32
136-
181+
ans := -1001
137182
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 {
140185
return 0
141186
}
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
146191
}
147-
148192
dfs(root)
149193
return ans
150194
}
@@ -157,39 +201,37 @@ func max(a, b int) int {
157201
}
158202
```
159203

160-
### **C++**
204+
### **TypeScript**
161205

162-
```cpp
206+
```ts
163207
/**
164208
* 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+
* }
173219
*/
174-
class Solution {
175-
public:
176-
int maxPathSum(TreeNode* root) {
177-
int ans = INT_MIN;
178220

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+
}
193235
```
194236

195237
### **JavaScript**
@@ -208,15 +250,15 @@ public:
208250
* @return {number}
209251
*/
210252
var maxPathSum = function (root) {
211-
let ans = -1000;
212-
let dfs = function (root) {
253+
let ans = -1001;
254+
const dfs = root => {
213255
if (!root) {
214256
return 0;
215257
}
216258
const left = Math.max(0, dfs(root.left));
217259
const right = Math.max(0, dfs(root.right));
218260
ans = Math.max(ans, left + right + root.val);
219-
return root.val + Math.max(left, right);
261+
return Math.max(left, right) + root.val;
220262
};
221263
dfs(root);
222264
return ans;
@@ -240,17 +282,17 @@ var maxPathSum = function (root) {
240282
* }
241283
*/
242284
public class Solution {
243-
private int ans;
285+
private int ans = -1001;
244286

245287
public int MaxPathSum(TreeNode root) {
246-
ans = int.MinValue;
247288
dfs(root);
248289
return ans;
249290
}
250291

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+
}
254296
int left = Math.Max(0, dfs(root.left));
255297
int right = Math.Max(0, dfs(root.right));
256298
ans = Math.Max(ans, left + right + root.val);

0 commit comments

Comments
 (0)
Please sign in to comment.