Skip to content

Commit fa9e690

Browse files
committed
Runtime: 15 ms (Top 57.67%) | Memory: 71 MB (Top 82.51%)
1 parent dd45f00 commit fa9e690

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
1+
// Runtime: 15 ms (Top 57.67%) | Memory: 71 MB (Top 82.51%)
12
class Solution {
2-
3+
34
int ans = 0;
45
public int maxSumBST(TreeNode root) {
56
solve(root);
67
return ans;
78
}
89
// int[] = { min, max, sum };
910
private int[] solve(TreeNode root) {
10-
if(root == null)
11+
if(root == null)
1112
return new int[] { Integer.MAX_VALUE, Integer.MIN_VALUE, 0 };
12-
13+
1314
int[] left = solve(root.left);
1415
int[] right = solve(root.right);
15-
16+
1617
if(root.val > left[1] && root.val < right[0]) {
1718
int sum = left[2] + right[2] + root.val;
1819
ans = Math.max(ans, sum);
19-
return new int[] { Math.min(left[0], root.val), Math.max(root.val, right[1]), sum };
20+
return new int[] { Math.min(left[0], root.val), Math.max(root.val, right[1]), sum };
2021
}
21-
22-
return new int[] { Integer.MIN_VALUE, Integer.MAX_VALUE, 0 };
22+
23+
return new int[] { Integer.MIN_VALUE, Integer.MAX_VALUE, 0 };
2324
}
2425

25-
}
26+
}

0 commit comments

Comments
 (0)