Java Solution with recursion


  • 7
    public class Solution {
    public boolean isValidBST(TreeNode root) {
       return isValidBST(root,null,null);
       
    }
    private boolean isValidBST(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 isValidBST(root.left,root.val,min)&&isValidBST(root.right,max,root.val); 
    }
    

    }


Log in to reply
 

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