時間復(fù)雜度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);
}
}