My java recursive solution using Integer.compareTo() method

  • 4

    At first, I use Integer.MAX_VALUE and MIN_VALUE as constraints that however did not pass the corner case...

    public class Solution {
        public boolean isValidBST(TreeNode root) {
            return valid(root, null, null);
        private boolean valid(TreeNode root, Integer lo, Integer hi) {
            if (root == null) return true;
            Integer val = root.val;
            if (lo != null && val.compareTo(lo) <= 0) return false;
            if (hi != null && val.compareTo(hi) >= 0) return false;
            return valid(root.left, lo, val) && valid(root.right, val, hi);

  • 0

    where the lo and hi get value in this code?

Log in to reply

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