156. Range Sum of BST

Easy · Binary Tree

Given the root of a binary search tree and two integers `low` and `high`, return the sum of all node values in the BST that fall within the inclusive range `[low, high]`.

A binary search tree (BST) is a tree where for each node, all values in its left subtree are less than the node's value, and all values in its right subtree are greater than the node's value.

Examples

Example 1
Input: root = [10, 5, 15, 3, 7, null, 18], low = 7, high = 15
Output: 32
Explanation: Nodes with values 7, 10, and 15 fall within [7, 15]. Their sum is 7 + 10 + 15 = 32.
Example 2
Input: root = [10, 5, 15, 3, 7, 13, 18, 1, null, 6], low = 6, high = 10
Output: 23
Explanation: Nodes with values 7, 6, and 10 fall within [6, 10]. Their sum is 7 + 6 + 10 = 23.

Constraints