java min max value tracking


  • 0
    R
    public boolean isValidBST(TreeNode root) 
    {
       if(root==null)
       return true;
       
       if(root.left==null && root.right==null)
       return true;
       
       if(root.val==Integer.MIN_VALUE && root.left!=null)
       return false;
       
       if(root.val==Integer.MAX_VALUE && root.right!=null)
       return false;
       
       boolean l = false;
        if(root.left==null)
        {
            l=true;
        }
        else
        {
           l=helperBST(root.left,Integer.MIN_VALUE,root.val-1);
        }
        
        boolean r=false;
        if(root.right==null)
        {
            r = true;
        }
        else
        {
          r=helperBST(root.right,root.val+1,Integer.MAX_VALUE);
        }
        
         if(l && r)
       return true;
       
       return false;
      
    }
    
    private boolean helperBST(TreeNode root,int min,int max)
    {
        if(root.val>max || root.val<min)
        return false;
        
         if(root.val==Integer.MIN_VALUE && root.left!=null)
       return false;
       
       if(root.val==Integer.MAX_VALUE && root.right!=null)
       return false;
       
        
        boolean l = false;
        if(root.left==null)
        {
            l=true;
        }
        else
        {
           l=helperBST(root.left,min,root.val-1);
        }
        
        boolean r=false;
        if(root.right==null)
        {
            r = true;
        }
        else
        {
          r=helperBST(root.right,root.val+1,max);
        }
        
         if(l && r)
       return true;
       
       return false;
    }

Log in to reply
 

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