Skip to content

Instantly share code, notes, and snippets.

@prepor
Created April 6, 2018 09:06
Show Gist options
  • Save prepor/e5656cb739a416ded3c89f98a26b7acf to your computer and use it in GitHub Desktop.
Save prepor/e5656cb739a416ded3c89f98a26b7acf to your computer and use it in GitHub Desktop.
open Base
open With_return
type node = { value: int; left: node option; right: node option }
let is_bst tree=
with_return (fun r ->
let rec tick = function
| { value; left = None; right = None} -> value
| { value; left = Some l; right = None} ->
if tick l <= value then value else r.return false
| { value; left = None; right = Some ri} ->
let a = tick ri in
if a > value then a else r.return false
| { value; left = Some l; right = Some ri;} ->
let a = tick ri in
if tick l <= value && a > value then a else r.return false in
tick tree |> ignore;
true)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment