-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode0124.java
More file actions
37 lines (32 loc) · 1.13 KB
/
Copy pathLeetCode0124.java
File metadata and controls
37 lines (32 loc) · 1.13 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
37
/* Binary Tree Maximum Path Sum
* Input:
* -10
/ \
9 20
/ \
15 7
* Output: 42
* */
public class LeetCode0124 {
public static int index = 0;
public static int[] TREE_VALUE = new int[]{-10, 9, 0, 0, 20, 15, 0, 0, 7, 0, 0};
public static void main(String args[]) {
TreeNode root = new TreeNode();
root = TreeNode.createTree(root, index, TREE_VALUE);
System.out.println(maxPathSum(root));
}
public static int max = Integer.MIN_VALUE;
public static int maxPathSum(TreeNode root) {
dfs(root);
return max;
}
public static int dfs(TreeNode root){
if(root == null)
return 0;
int leftMax = Math.max(dfs(root.left), 0);
int rightMax = Math.max(dfs(root.right), 0);
int pathValue = root.val + leftMax + rightMax; //包含当前根节点的完整路径值
max = Math.max(max, pathValue); //更新max
return root.val + Math.max(leftMax, rightMax); //返回当前节点能给上层贡献的最大值
}
}