-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxPathSum.java
More file actions
36 lines (34 loc) · 1.16 KB
/
Copy pathMaxPathSum.java
File metadata and controls
36 lines (34 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package LeetCode;
/**
* @FileName: MaxPathSum.java
* @Description: 二叉树中的最大路径和
* @Author: ABCpril
* @Date: 2022/02/10
*/
public class MaxPathSum {
public int maxPathSum(TreeNode root) {
dfs(root);
return maxSum;
}
private int maxSum = Integer.MIN_VALUE;
// 1.递归的含义:返回当前节点能为父节点提供的贡献值
private int dfs(TreeNode root) {
// 3.递归的出口:如果当前节点为null,对父节点贡献为0
if (root == null) return 0;
// 2.递归的拆解
int leftRes = dfs(root.left);
int rightRes = dfs(root.right);
// 左右子树结果对最终结果的作用
// a
// / \
// b c
// b + a + c。
// b + a + a 的父结点。
// a + c + a 的父结点。
maxSum = Math.max(maxSum, root.val + leftRes + rightRes);
// 如果走root上去的路线,root节点能为父节点提供的最大贡献
int outputMaxSum = Math.max(root.val + leftRes, root.val + rightRes);
// 如果贡献 < 0,直接返回 0
return outputMaxSum < 0 ? 0 : outputMaxSum;
}
}