-
Notifications
You must be signed in to change notification settings - Fork 21
/
1.java
48 lines (44 loc) · 1.66 KB
/
1.java
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
class Solution {
// 用一个类包装递归函数的四个返回值
class TreeInfo {
boolean isBST;
int min;
int max;
int sum;
TreeInfo(boolean isBST, int min, int max, int sum) {
this.isBST = isBST;
this.min = min;
this.max = max;
this.sum = sum;
}
}
int maxSum; // 全局变量:记录二叉搜索子树的结点之和的最大值
public int maxSumBST(TreeNode root) {
maxSum = 0;
traverse(root);
return maxSum;
}
TreeInfo traverse(TreeNode root) {
// base case:空子树是二叉搜索树,最小值、最大值不存在,和为 0
if (root == null) {
return new TreeInfo(true, Integer.MAX_VALUE, Integer.MIN_VALUE, 0);
}
// 递归计算左右子树的子问题
TreeInfo left = traverse(root.left);
TreeInfo right = traverse(root.right);
// 套用公式:计算结点之和
int sum = root.val + left.sum + right.sum;
// 套用公式:判断是否是二叉搜索树
if (left.isBST && right.isBST && left.max < root.val && root.val < right.min) {
// 当前子树是二叉搜索树的情况
maxSum = Math.max(maxSum, sum);
// 套用公式:计算二叉树最小值和最大值
int min = Math.min(left.min, root.val);
int max = Math.max(right.max, root.val);
return new TreeInfo(true, min, max, sum);
} else {
// 当前子树不是二叉搜索树的情况
return new TreeInfo(false, Integer.MAX_VALUE, Integer.MIN_VALUE, sum);
}
}
}