#98.Validate Binary Search Tree [LeetCode Grind 75 in Java]

Kallol BairagiKallol Bairagi
1 min read
class Solution {

    public boolean isValid(TreeNode root, long minLimit, long maxLimit){
        if(root == null) return true;
        if(root.val <= minLimit || root.val >= maxLimit) return false;
        return (isValid(root.left, minLimit, root.val) && isValid(root.right, root.val, maxLimit));
    }

    public boolean isValidBST(TreeNode root) {
        return isValid(root, Long.MIN_VALUE, Long.MAX_VALUE);
    }
}
0
Subscribe to my newsletter

Read articles from Kallol Bairagi directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Kallol Bairagi
Kallol Bairagi