输出: 3 解释: 高亮部分为最大的 BST 子树。 返回值 3 在这个样例中为子树大小。 进阶: 你能想出用 O(n) 的时间复杂度解决这个问题吗?
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
int result;
private class Info{
int max;
int min;
int count;
boolean isBST;
Info(int max,int min,boolean isBST,int count){
this.max=max;
this.min=min;
this.isBST=isBST;
this.count=count;
}
Info(){}
};
public int largestBSTSubtree(TreeNode root) {
if(root==null){
return 0;
}
recur(root);
return result;
}
private Info recur(TreeNode root){
if(root==null){
return new Info(Integer.MIN_VALUE,Integer.MAX_VALUE,true,0);
}
Info left=recur(root.left);
Info right=recur(root.right);
//先判断左子树和右子树是不是BST,如果不是,本层也不用判断了
if(!left.isBST || !right.isBST){
return new Info(Integer.MIN_VALUE,Integer.MAX_VALUE,false,0);
}
//左右子树都是BST,判断本层是不是BST,并且需要更新result
if(root.val<=left.max || root.val>=right.min){
return new Info(Integer.MIN_VALUE,Integer.MAX_VALUE,false,0);
}
//本层已经是BST了,更新最大BST的节点数
result=Math.max(result,left.count+right.count+1);
Info info=new Info();
info.count=left.count+right.count+1;
info.isBST=true;
//收集当前层的最大值与最小值
info.min=Math.min(root.val,Math.min(left.min,right.min));
info.max=Math.max(root.val,Math.max(left.max,right.max));
// if(root.left== null && root.right==null){
// info.max=root.val;
// info.min=root.val;
// }
// //这里如果不使用else,那么如果左右节点都为空,还是会执行最后一个else
// else if(root.left!=null && root.right!=null){
// info.min=left.min;
// info.max=right.max;
// }else if(root.left!=null){
// info.min=left.min;
// info.max=root.val;
// }else{
// info.min=root.val;
// info.max=right.max;
// }
return info;
}
}