LeetCode 230. Kth Smallest Element in a BST in F#
URL
leetcode.com/problems/kth-smallest-element-..
Code
github.com/syohex/dotnet-study/blob/master/..
type Tree =
| Leaf
| Node of int * Tree * Tree
let kthSmallest (root: Tree) (k: int) : int =
let rec kthSmallest' node acc =
match node with
| Leaf -> acc
| Node (v, left, right) ->
let acc' = kthSmallest' left acc
kthSmallest' right (v :: acc')
kthSmallest' root []
|> List.rev
|> List.item (k - 1)