Skip to content

Instantly share code, notes, and snippets.

@dmitry-a-morozov
Last active February 20, 2019 18:29
Show Gist options
  • Save dmitry-a-morozov/970086c1f2eb7aeb6d9232515abc87a9 to your computer and use it in GitHub Desktop.
Save dmitry-a-morozov/970086c1f2eb7aeb6d9232515abc87a9 to your computer and use it in GitHub Desktop.
Kth largest in BST
type Node<'T> =
| Node of value: 'T * left: Node<'T> * right: Node<'T>
| Empty
let getKthLargest(root, n) =
let rec loop = function
| Empty -> Seq.empty
| Node(value, left, right) ->
seq {
yield! loop right
yield value
yield! loop left
}
root |> loop |> Seq.item (n - 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment