LeetCode 1261. Find Elements in a Contaminated Binary Tree in F#
URL
Find Elements in a Contaminated Binary Tree - LeetCode
Code
type Tree =
| Leaf
| Node of int * Tree * Tree
type FindElement =
{ Vals: Set<int> }
static member init(root: Tree) : FindElement =
{ Vals = FindElement.initValues 0 root Set.empty }
static member initValues v node acc =
let acc = Set.add v acc
match node with
| Leaf -> acc
| Node(_, left, right) ->
let acc =
match left with
| Leaf -> acc
| _ -> FindElement.initValues (2 * v + 1) left acc
match right with
| Leaf -> acc
| _ -> FindElement.initValues (2 * v + 2) right acc
static member find v (fe: FindElement) : bool = Set.contains v fe.Vals