女大厨不会做糖醋排骨:工签遭拒 (转载)# Joke - 肚皮舞运动
h*3
1 楼
Largest BST Subtree 的 best solution, 哪位大神给我讲讲最后那个 result 是怎
么构造的?为什么是那么构造的?
public class Solution {
class Result {
int res;
int min;
int max;
public Result(int res, int min, int max) {
this.res = res;
this.min = min;
this.max = max;
}
}
public int largestBSTSubtree(TreeNode root) {
Result res = BSTSubstree(root);
return Math.abs(res.res);
}
private Result BSTSubstree(TreeNode root) {
if (root == null) return new Result(0, Integer.MAX_VALUE, Integer.
MIN_VALUE);
Result left = BSTSubstree(root.left);
Result right = BSTSubstree(root.right);
if (left.res < 0 || right.res < 0 || root.val < left.max || root.val
> right.min) {
return new Result(Math.max(Math.abs(left.res), Math.abs(right.
res)) * -1, 0, 0);
} else {
return new Result(left.res + right.res + 1, Math.min(root.val,
left.min), Math.max(root.val, right.max));
}
}
}
么构造的?为什么是那么构造的?
public class Solution {
class Result {
int res;
int min;
int max;
public Result(int res, int min, int max) {
this.res = res;
this.min = min;
this.max = max;
}
}
public int largestBSTSubtree(TreeNode root) {
Result res = BSTSubstree(root);
return Math.abs(res.res);
}
private Result BSTSubstree(TreeNode root) {
if (root == null) return new Result(0, Integer.MAX_VALUE, Integer.
MIN_VALUE);
Result left = BSTSubstree(root.left);
Result right = BSTSubstree(root.right);
if (left.res < 0 || right.res < 0 || root.val < left.max || root.val
> right.min) {
return new Result(Math.max(Math.abs(left.res), Math.abs(right.
res)) * -1, 0, 0);
} else {
return new Result(left.res + right.res + 1, Math.min(root.val,
left.min), Math.max(root.val, right.max));
}
}
}