Simple java solution - recursive validation


  • 5
    T
    public class Solution {
    public boolean isValidBST(TreeNode root) {
        return isValidBST(root, (long)Integer.MIN_VALUE, (long)Integer.MAX_VALUE);
    }
    
    public boolean isValidBST(TreeNode root, long min, long max) {
        if(root == null) return true;
        long val = root.val;
        return (val >= min && val <= max && isValidBST(root.left, min, val -1) && isValidBST(root.right, val+1, max));
    }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.