LeetCode 513. Find Bottom Left Tree Value in F#

URL

https://leetcode.com/problems/find-bottom-left-tree-value/description/?envType=daily-question&envId=2024-02-28

Code

https://github.com/syohex/dotnet-study/blob/master/fsharp/leetcode/challenge/202402/find_bottom_left_tree_value/main.fsx

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

let findLeftBottom (root: Tree) : int =
    let rec findLeftBottom' q acc =
        match q with
        | [] -> acc
        | Leaf :: _ -> failwith "never reach here"
        | Node(v, _, _) :: _ ->
            let q' =
                q
                |> List.fold
                    (fun acc node ->
                        match node with
                        | Leaf -> acc
                        | Node(_, left, right) -> right :: left :: acc)
                    []
                |> List.filter (fun node -> node <> Leaf)
                |> List.rev

            findLeftBottom' q' v

    findLeftBottom' [ root ] 0