Shohei Yoshida
Shohei Yoshida's Blog

Follow

Shohei Yoshida's Blog

Follow

LeetCode 938. Range Sum of BST in F#

Shohei Yoshida's photo
Shohei Yoshida
·Dec 7, 2022·

1 min read

URL

leetcode.com/problems/range-sum-of-bst/desc..

Code

github.com/syohex/dotnet-study/blob/master/..

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

let rangeSumBST (root: Tree) (low: int) (high: int) : int =
    let rec rangeSumBST' node low high acc =
        match node with
        | Leaf -> acc
        | Node(v, left, right) ->
            let acc' = if v >= low && v <= high then acc + v else acc
            let acc'' = rangeSumBST' left low high acc'
            rangeSumBST' right low high acc''

    rangeSumBST' root low high 0
 
Share this