Share my javasolutions


  • 0
    S
    public class Solution {
        public boolean isValidBST(TreeNode root) {
            return isValidBSTHelper(root, 0,0,false,false);
        }
       private boolean isValidBSTHelper(TreeNode root, int min, int max, boolean min_set, boolean max_set) {
            if(root == null) return true;
            // handle case where the integer boundary reached.
            if (min-1>min || max+1 < max) return false;
            boolean left, right;
            left = isValidBSTHelper(root.left, min, root.val-1, min_set, true);
            right = isValidBSTHelper(root.right, root.val+1, max, true, max_set);
            return left && right && ((root.val >= min) || !min_set) && ((root.val <= max)|| ! max_set);
               
            
        }
    }

Log in to reply
 

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