98. Validate Binary Search Tree

2016-11-24  本文已影响0人  夜皇雪

时间复杂度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);
    }
}
上一篇 下一篇

猜你喜欢

热点阅读