LeetCode 1325. Delete Leaves With a Given Value in F#

URL

Delete Leaves With a Given Value - LeetCode

Code

https://github.com/syohex/dotnet-study/tree/master/fsharp/leetcode/challenge/202405/delete_leaves_with_a_given_value/main.fsx

type Tree =
    | Leaf
    | Node of int * Tree * Tree

let rec removeLeafNodes (root: Tree) (target: int) : Tree =
    match root with
    | Leaf -> Leaf
    | Node(v, Leaf, Leaf) when v = target -> Leaf
    | Node(v, left, right) ->
        let left' = removeLeafNodes left target
        let right' = removeLeafNodes right target

        match left', right', v = target with
        | Leaf, Leaf, true -> Leaf
        | _ -> Node(v, left', right')