LeetCode 199. Binary Tree Right Side View in F#
URL
leetcode.com/problems/binary-tree-right-sid..
Code
github.com/syohex/dotnet-study/blob/master/..
type Tree =
| Leaf
| Node of int * Tree * Tree
let rightSideView (root: Tree) : int list =
let rec rightSideView' q ret =
match q with
| [] -> ret |> List.rev
| _ ->
let q', v =
q
|> List.fold
(fun (acc, prev) n ->
match n with
| Leaf -> acc, prev
| Node (v, left, right) ->
match left, right with
| Leaf, Leaf -> acc, v
| Node (_), Leaf -> (left :: acc), v
| Leaf, Node (_) -> (right :: acc), v
| Node (_), Node (_) -> (right :: left :: acc), v)
([], -1)
rightSideView' (q' |> List.rev) (v :: ret)