-
Notifications
You must be signed in to change notification settings - Fork 42
/
solution.js
55 lines (54 loc) · 1.39 KB
/
solution.js
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
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {number}
*/
var maxSumBST = function (root) {
const rst = postOrder(root)[0];
return rst > 0 ? rst : 0;
};
function postOrder (root) {
if (!root) {
return [
-Infinity, true, -Infinity, Infinity, 0, ];
}
if (!root.left && !root.right) {
return [
root.val, true, root.val, root.val, root.val, ];
}
let isBST = true;
let result = -Infinity;
let min = root.val;
let max = root.val;
let cur = root.val;
if (root.left) {
const [
leftRst, leftIsBST, leftMin, leftMax, leftSum, ] = postOrder(root.left);
result = Math.max(result, leftRst);
if (!leftIsBST || root.val <= leftMax) {
isBST = false;
} else {
cur += leftSum;
}
min = leftMin;
}
if (root.right) {
const [
rightRst, rightIsBST, rightMin, rightMax, rightSum, ] = postOrder(root.right);
result = Math.max(result, rightRst);
if (!rightIsBST || root.val >= rightMin) {
isBST = false;
} else {
cur += rightSum;
}
max = rightMax;
}
return [
Math.max(result, isBST ? cur : -Infinity), isBST, min, max, cur, ];
}