Java solution using min/max


  • 0
    J

    Using the property left <=current <right

     public boolean isValidBST(TreeNode root) {
        
            return valid(root, null, null);
        }
        
        public static boolean valid(TreeNode root, Integer min, Integer max){
            
            if(root==null)
            {
                return true;
            }
            if((min!=null && root.val <= min) || (max!=null && root.val >=max))
            {
                return false;
            }
            if(!valid(root.left,min,root.val) || !valid(root.right,root.val,max)){
                return false;
            }
            return true;
        }
    

Log in to reply
 

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