LeetCode 938. Range Sum of BST in F#
URL
https://leetcode.com/problems/range-sum-of-bst/description/?envType=daily-question&envId=2024-01-08
Code
type Tree =
| Leaf
| Node of int * Tree * Tree
let rangeSumBST (root: Tree) (low: int) (high: int) : int =
let rec rangeSumBST' node low high =
match node with
| Leaf -> 0
| Node(v, left, right) ->
let sumLeft = rangeSumBST' left low high
let sumRight = rangeSumBST' right low high
if v >= low && v <= high then
v + sumLeft + sumRight
else
sumLeft + sumRight
rangeSumBST' root low high