Skip to content

Commit d1ecd07

Browse files
committed
二刷124
1 parent 51458b5 commit d1ecd07

File tree

6 files changed

+103
-32
lines changed

6 files changed

+103
-32
lines changed

README.adoc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -894,12 +894,12 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
894894
|Hard
895895
|
896896

897-
//|{counter:codes}
898-
//|{leetcode_base_url}/binary-tree-maximum-path-sum/[124. Binary Tree Maximum Path Sum^]
899-
//|{source_base_url}/_0124_BinaryTreeMaximumPathSum.java[Java]
900-
//|{doc_base_url}/0124-binary-tree-maximum-path-sum.adoc[题解]
901-
//|Hard
902-
//|
897+
|{counter:codes}
898+
|{leetcode_base_url}/binary-tree-maximum-path-sum/[124. Binary Tree Maximum Path Sum^]
899+
|{source_base_url}/_0124_BinaryTreeMaximumPathSum.java[Java]
900+
|{doc_base_url}/0124-binary-tree-maximum-path-sum.adoc[题解]
901+
|Hard
902+
|
903903

904904
|{counter:codes}
905905
|{leetcode_base_url}/valid-palindrome/[125. Valid Palindrome^]

docs/0000-16-depth-first-search.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
树形DFS基于深搜(Depth First Search (DFS))技术来实现树的遍历。
77

8-
咱们可以用递归(或是显示栈,如果你想用迭代方式的话)来记录遍历过程中访问过的父节点。
8+
咱们可以用递归(或是显式栈,如果你想用迭代方式的话)来记录遍历过程中访问过的父节点。
99

1010
该模式的运行方式是从根节点开始,如果该节点不是叶子节点,我们需要干三件事:
1111

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,65 @@
11
[#0124-binary-tree-maximum-path-sum]
2-
= 124. Binary Tree Maximum Path Sum
2+
= 124. 二叉树中的最大路径和
33

4-
{leetcode}/problems/binary-tree-maximum-path-sum/[LeetCode - Binary Tree Maximum Path Sum^]
4+
https://leetcode.cn/problems/binary-tree-maximum-path-sum/[LeetCode - 124. 二叉树中的最大路径和 ^]
55

6-
Given a *non-empty* binary tree, find the maximum path sum.
6+
二叉树中的 *路径* 被定义为一条节点序列,序列中每对相邻节点之间都存在一条边。同一个节点在一条路径序列中 *至多出现一次* 。该路径 *至少包含一个* 节点,且不一定经过根节点。
77

8-
For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path must contain *at least one node* and does not need to go through the root.
8+
*路径和* 是路径中各节点值的总和。
99

10-
*Example 1:*
10+
给你一个二叉树的根节点 `root` ,返回其 *最大路径和*
1111

12-
[subs="verbatim,quotes,macros"]
13-
----
14-
*Input:* [1,2,3]
15-
16-
*1*
17-
*/ \*
18-
*2* *3*
19-
20-
*Output:* 6
21-
----
12+
*示例 1:*
2213

23-
*Example 2:*
14+
....
15+
1
16+
/ \
17+
2 3
18+
输入:root = [1,2,3]
19+
输出:6
20+
解释:最优路径是 2 -> 1 -> 3 ,路径和为 2 + 1 + 3 = 6
21+
....
2422

25-
[subs="verbatim,quotes,macros"]
26-
----
27-
*Input:* [-10,9,20,null,null,15,7]
23+
*示例 2:*
2824

25+
....
2926
-10
3027
/ \
31-
9 *20*
32-
*/ \*
33-
*15 7*
28+
9 20
29+
/ \
30+
15 7
3431
35-
*Output:* 42
36-
----
32+
输入:root = [-10,9,20,null,null,15,7]
33+
输出:42
34+
解释:最优路径是 15 -> 20 -> 7 ,路径和为 15 + 20 + 7 = 42
35+
....
3736

37+
*提示:*
38+
39+
* 树中节点数目范围是 `[1, 3 * 10^4^]`
40+
* `+-1000 <= Node.val <= 1000+`
41+
42+
== 思路分析
3843

3944

4045
[[src-0124]]
46+
[tabs]
47+
====
48+
一刷::
49+
+
50+
--
4151
[{java_src_attr}]
4252
----
4353
include::{sourcedir}/_0124_BinaryTreeMaximumPathSum.java[tag=answer]
4454
----
55+
--
4556
57+
二刷::
58+
+
59+
--
60+
[{java_src_attr}]
61+
----
62+
include::{sourcedir}/_0124_BinaryTreeMaximumPathSum_2.java[tag=answer]
63+
----
64+
--
65+
====

logbook/202503.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ endif::[]
9595
|{doc_base_url}/0102-binary-tree-level-order-traversal.adoc[题解]
9696
|✅ 广度优先搜索
9797

98+
|{counter:codes2503}
99+
|{leetcode_base_url}/binary-tree-maximum-path-sum/[124. Binary Tree Maximum Path Sum^]
100+
|{doc_base_url}/0124-binary-tree-maximum-path-sum.adoc[题解]
101+
|⭕️ 深度优先搜索,注意处理负数情况
102+
98103
|===
99104

100105
截止目前,本轮练习一共完成 {codes2503} 道题。

src/main/java/com/diguage/algo/leetcode/_0124_BinaryTreeMaximumPathSum.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
*/
1414
public class _0124_BinaryTreeMaximumPathSum {
1515
// tag::answer[]
16-
16+
/**
17+
* @author D瓜哥 · https://www.diguage.com
18+
* @since 2020-04-01 23:09
19+
*/
1720
private int result = Integer.MIN_VALUE;
1821

1922
public int maxPathSum(TreeNode root) {
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import com.diguage.algo.util.TreeNode;
4+
5+
public class _0124_BinaryTreeMaximumPathSum_2 {
6+
// tag::answer[]
7+
/**
8+
* @author D瓜哥 · https://www.diguage.com
9+
* @since 2025-04-01 10:57:14
10+
*/
11+
int result = Integer.MIN_VALUE;
12+
13+
public int maxPathSum(TreeNode root) {
14+
dfs(root);
15+
return result;
16+
}
17+
18+
private int dfs(TreeNode root) {
19+
if (root == null) {
20+
return 0;
21+
}
22+
int left = dfs(root.left);
23+
int right = dfs(root.right);
24+
// 对于左右子树,这里有如下几种组合
25+
// 1. root.val (left 和 right 都是负数)
26+
// 2. root.val + Math.max(left, right) (left 和 right 有一个是正数)
27+
// 3. left + root.val + right (left 和 right 都是正数)
28+
result = Math.max(result, Math.max(left + root.val + right,
29+
Math.max(Math.max(left, right) + root.val, root.val)));
30+
// 对于返回给父节点,这里有两种可能
31+
// 1. root.val (left 和 right 都是负数)
32+
// 2. root.val + Math.max(left, right) (left 和 right 有一个是正数)
33+
// 如果 max 方法支持传递多个参数,可以简写为:Math.max(0, left, right) + root.val
34+
return Math.max(root.val, Math.max(left, right) + root.val);
35+
}
36+
// end::answer[]
37+
38+
public static void main(String[] args) {
39+
TreeNode root = new TreeNode(2);
40+
root.left = new TreeNode(-1);
41+
new _0124_BinaryTreeMaximumPathSum_2().maxPathSum(root);
42+
}
43+
}

0 commit comments

Comments
 (0)