https://kotlinlang.org logo
#getting-started
Title
# getting-started
o

oday

02/01/2021, 2:39 PM
how can I translate this to Kotlin?
Copy code
class Solution(object):
  def _isValidBSTHelper(self, n, low, high):
    if not n:
      return True
    val = n.val
    if ((val > low and val < high) and
        self._isValidBSTHelper(n.left, low, n.val) and
        self._isValidBSTHelper(n.right, n.val, high)):
        return True
    return False

  def isValidBST(self, n):
    return self._isValidBSTHelper(n, float('-inf'), float('inf'))
my attempt produced this
Copy code
fun isValidChecker(
    node: Node<Int>?, low: Int, high: Int
): Boolean {
    if (node == null)
        return true

    val value = node.value

    if (value in (low + 1) until high
        && isValidChecker(node.left, low, value)
        && isValidChecker(node.right, value, high)
    )
        return true

    return false
}

fun isValidBST(node: Node<Int>?): Boolean {
    return isValidChecker(
        node, Int.MIN_VALUE, Int.MAX_VALUE
    )
}
r

Ruckus

02/01/2021, 3:53 PM
Looks reasonable to me. If you really wanted you could shrink it down to something like:
Copy code
fun Node<Int>?.isValidBST(
    low: Int = Int.MIN_VALUE,
    hight: Int = Int.MAX_VALUE,
): Boolean = return this == null ||
    value in (low + 1) until high &&
    left.isValidBST(low, value) &&
    right.isValidBST(value, high)
o

oday

02/01/2021, 3:53 PM
issue is what to feed isValidBST
I want negative infinity and positive infinity with the current way that the code looks
r

Ruckus

02/01/2021, 3:55 PM
You don't need +/- infinity. An int can never be outside the range of MIN_VALUE..MAX_VALUE, so that is effectively infinite bounds.
The only issue is the fact that MIN_VALUE will never be valid, so you may need to change the logic a bit.
o

oday

02/01/2021, 3:55 PM
I’m with you logically, but running the above Python variation of it makes this LeetCode question pass, running our Kotlin version fails on 7 cases
so something is off
r

Ruckus

02/01/2021, 3:57 PM
Maybe something like
Copy code
fun Node<Int>?.isValidBST(
    low: Int = Int.MIN_VALUE,
    hight: Int = Int.MAX_VALUE,
): Boolean = this == null ||
    value <= low &&
    value >= high &&
    left.isValidBST(low, value - 1) &&
    right.isValidBST(value + 1, high)
o

oday

02/01/2021, 4:02 PM
can’t we use an operator that is exclusive?
instead of the +1 and -1 ?
dont get that part
r

Ruckus

02/01/2021, 4:05 PM
Not if you use finite bounds. You can either use ints and modify the logic accordingly or use floating point values and keep the logic the same.
If you want to use floating point values, it would be as simple as changing the line in your original code to
Copy code
val value = node.value.toDouble()
And accepting
Doubles
for the low / high params, using
Double.NEGATIVE_INFINITY
and
Double.POSITIVE_INFINITY
as the initial bounds.
(You could of course use `Float`s as well.)
o

oday

02/01/2021, 4:45 PM
think I broke it
Copy code
class Solution {
    fun TreeNode?.isValidBST(
        low: Double = Double.NEGATIVE_INFINITY, high: Double = Double.POSITIVE_INFINITY
    ): Boolean = this == null || 
    (`val`.toDouble() > low && `val`.toDouble() < high)
    && left.isValidBST(low, `val`.toDouble())
    && right.isValidBST(`val`.toDouble(), high)

    fun isValidBST(root: TreeNode?): Boolean {
        return root.isValidBST()
    }
}
this works on Leetcode
i insisted on having it as an extension lol
4 Views