← 返回 meta 的题目列表Range Sum of BST
类型:qbank
LeetCode 938. DFS through a BST summing nodes whose values fall in `[low, high]`. Pruning via BST property is the discussion point.
Requirements
Given the root of a BST and a range [low, high], return the sum of all node values in range.
DFS: prune left subtree when node.val < low; prune right when node.val > high.
Follow-up reported by one L6 onsite: pair with a streaming "product of last K values" interface.
Notes
BST property is what makes this O(log n + count) on average rather than O(n).
Iterative stack version is fine, but recursive is cleaner under time pressure.
Preparation
Write recursive solution in <5 min.
For the L6 paired interface, sketch a circular buffer + running-product class with rebuild on zero-insertion.