LeetCode 113. Path Sum II in F#
URL
leetcode.com/problems/path-sum-ii
Code
github.com/syohex/dotnet-study/blob/master/..
type Tree =
| Leaf
| Node of int * Tree * Tree
let pathSum (root: Tree) (targetSum: int) : int list list =
let rec pathSum' node sum targetSum vals acc =
match node with
| Leaf ->
if sum = targetSum then
Set.add (vals |> List.rev) acc
else
acc
| Node (v, left, right) ->
let vals' = v :: vals
let sum' = v + sum
let acc' = pathSum' left sum' targetSum vals' acc
pathSum' right sum' targetSum vals' acc'
pathSum' root 0 targetSum [] Set.empty
|> Set.toList