Shohei Yoshida
Shohei Yoshida's Blog

Shohei Yoshida's Blog

LeetCode 98. Validate Binary Search Tree in F#

Shohei Yoshida's photo
Shohei Yoshida
·Aug 11, 2022·

1 min read

URL

leetcode.com/problems/validate-binary-searc..

Code

github.com/syohex/dotnet-study/blob/master/..

open System

type Tree =
    | Leaf
    | Node of int64 * Tree * Tree

let isValidBST (root: Tree) : bool =
    let rec isValidBST' node (min: int64) (max: int64) =
        match node with
        | Leaf -> true
        | Node (v, left, right) ->
            if v <= min || v >= max then
                false
            else
                isValidBST' left min v && isValidBST' right v max

    isValidBST' root Int32.MinValue Int32.MaxValue
 
Share this