-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBinaryTreeMaximumPathSum.java
More file actions
91 lines (61 loc) · 1.55 KB
/
BinaryTreeMaximumPathSum.java
File metadata and controls
91 lines (61 loc) · 1.55 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package binary_tree;
import sun.reflect.generics.tree.Tree;
/**
* @Author: Wenhang Chen
* @Description: 给定一个非空二叉树,返回其最大路径和。
* <p>
* 本题中,路径被定义为一条从树中任意节点出发,达到任意节点的序列。该路径至少包含一个节点,且不一定经过根节点。
* 示例 1:
* <p>
* 输入: [1,2,3]
* <p>
* 1
* / \
* 2 3
* <p>
* 输出: 6
* <p>
* 示例 2:
* <p>
* 输入: [-10,9,20,null,null,15,7]
* <p>
* -10
* / \
* 9 20
* / \
* 15 7
* <p>
* 输出: 42
* @Date: Created in 11:51 12/20/2019
* @Modified by:
*/
public class BinaryTreeMaximumPathSum {
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
int val = Integer.MIN_VALUE;
public int maxPathSum(TreeNode root) {
getMaxPath(root);
return val;
}
public int getMaxPath(TreeNode root) {
if (root == null) return 0;
int left = getMaxPath(root.left);
int right = getMaxPath(root.right);
// a
// / \
// b c
// 一型:bac的情况
int lmr = root.val + Math.max(0, left) + Math.max(0, right);
// 二型:ac和a的父节点或ab和a的父节点的情况
int ret = root.val + Math.max(0, Math.max(left, right));
val = Math.max(val, Math.max(lmr, ret));
// 最终返回当前子树的二型最大路径长度
return ret;
}
}