-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode124.java
More file actions
42 lines (34 loc) · 1.29 KB
/
LeetCode124.java
File metadata and controls
42 lines (34 loc) · 1.29 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
38
39
40
41
42
import util.TreeNode;
public class LeetCode124 {
public static void main(String[] args) {
// 输入:root = [1,2,3]
// 输出:6
System.out.println(new Solution124().maxPathSum(TreeNode.buildTree(new Integer[] { 1, 2, 3 })));
// 输入:root = [-10,9,20,null,null,15,7]
// 输出:42
System.out.println(
new Solution124().maxPathSum(TreeNode.buildTree(new Integer[] { -10, 9, 20, null, null, 15, 7 })));
// 输入:root = [-3]
// 输出:-3
System.out.println(
new Solution124().maxPathSum(TreeNode.buildTree(new Integer[] { -3 })));
// 输入:root = [2,-1]
// 输出:2
System.out.println(
new Solution124().maxPathSum(TreeNode.buildTree(new Integer[] { 2, -1 })));
}
}
class Solution124 {
public int max = Integer.MIN_VALUE;
public int maxPathSum(TreeNode root) {
dfs(root);
return max;
}
public int dfs(TreeNode node) {
int left = node.left != null ? dfs(node.left) : 0;
int right = node.right != null ? dfs(node.right) : 0;
this.max = Math.max(left + right + node.val, max);
// NOTE: 存在不上传的情况
return Math.max(Math.max(left + node.val, right + node.val), 0);
}
}