LeetCode 173. Binary Search Tree Iterator in F#
URL
leetcode.com/problems/binary-search-tree-it..
Code
github.com/syohex/dotnet-study/blob/master/..
type Tree =
| Leaf
| Node of int * Tree * Tree
type BSTIterator =
{ Nums: int []
mutable Index: int }
static member New(root: Tree) : BSTIterator =
let rec collect node acc =
match node with
| Leaf -> acc
| Node (n, left, right) ->
let acc' = collect left acc
collect right (n :: acc')
let nums =
collect root [] |> List.rev |> List.toArray
{ Nums = nums; Index = 0 }
member this.Next() : int =
let ret = this.Nums.[this.Index]
this.Index <- this.Index + 1
ret
member this.HasNext() : bool = this.Index < this.Nums.Length
This problem is not suit for F#