美文网首页
98. Validate Binary Search Tree

98. Validate Binary Search Tree

作者: 夜皇雪 | 来源:发表于2016-11-24 07:05 被阅读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);
    }
}

相关文章

网友评论

      本文标题:98. Validate Binary Search Tree

      本文链接:https://www.haomeiwen.com/subject/qghbpttx.html