← 返回 meta 的题目列表Sum of BST Nodes Within Given Range
类型:online_judge
meta
Given a Binary Search Tree, find all nodes within a given range
Given a Binary Search Tree (BST) and two integers L and R, return the sum of the values of all nodes with a value in the range [L, R].
Example
Input: root = [10, 5, 15, 3, 7, null, 18], L = 7, R = 15
Output: 32
Explanation: Nodes with values in [7, 15] are 7, 10, and 15, so their sum is 32.
Constraints
The number of nodes in the tree is in the range [1, 10000]
Each node's value is in the range [0, 10000]
L and R are in the range [0, 10000], where L <= R
Example
Input
root = [10, 5, 15, 3, 7, null, 18], L = 7, R = 15