The Range Sum of BST problem asks you to find the sum of all node values in a Binary Search Tree that fall within a given range [low, high]. This easy coding problem uses BST-aware DFS — pruning subtrees that are entirely out of range. The binary search tree, DFS, binary tree, and tree interview pattern is demonstrated.
Microsoft, Meta, Amazon, Google, Bloomberg, and Adobe ask this because it tests understanding of BST properties — specifically that you can prune entire subtrees (left subtree if node > high, right subtree if node < low). This makes the solution more efficient than a full traversal.
BST-aware DFS with pruning. At each node: if node.val > high, go only left (all right subtree values too large). If node.val < low, go only right (all left subtree values too small). If in range, add to sum and traverse both children.
BST: root=10, left=5(3,7), right=15(13,18). Range=[7,15].
Range Sum of BST is the gateway to BST-aware algorithms. The core property: left subtree < node < right subtree. This enables directional pruning. Practice: "k-th smallest in BST," "count nodes in BST range," "sum of BST nodes." Each uses the same BST pruning logic. BST traversal is a fundamental skill tested at every major tech company.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Find Mode in Binary Search Tree | Easy | Solve | |
| Recover Binary Search Tree | Medium | Solve | |
| Convert BST to Greater Tree | Medium | Solve | |
| Lowest Common Ancestor of a Binary Search Tree | Medium | Solve | |
| Trim a Binary Search Tree | Medium | Solve |