时间复杂度O(n)
如果是遍历了所有点,就是O(n),如果是每层只遍历一个点,left,right,是O(logn)
public class Solution {
public boolean isValidBST(TreeNode root) {
if(root==null) return true;
return helper(root,null,null);
}
public boolean helper(TreeNode root,Integer max,Integer min){
if(root==null) return true;
if(max!=null&&root.val>=max) return false;
if(min!=null&&root.val<=min) return false;
return helper(root.left,root.val,min)&&helper(root.right,max,root.val);
}
}