My first instinct was to do a full traversal and just skip nodes outside the range, which works but ignores the BST property entirely.
Use the BST property to prune branches: if a node's value is less than low, only its right subtree can contain values in range; if greater than high, only its left subtree. Recursively sum values that fall within [low, high], visiting only necessary nodes. This yields O(n) worst-case but often much faster.
Pro tip: Mention that the algorithm runs in O(n) worst-case but typically O(k + h) where k is the number of nodes in range and h is the tree height, and note that it uses O(h) space for recursion. This shows you understand the practical performance beyond big-O.
Confirm that the tree is a BST, the range is inclusive, and node values are integers. Ask if the tree can be empty or if low > high.
Explain that you'll traverse the tree, pruning subtrees that cannot contain values in range based on BST ordering.
Write a function that takes a node and returns the sum. If node is null, return 0. If node.val < low, recurse right; if node.val > high, recurse left; else include node.val and recurse both sides.
State that time complexity is O(n) worst-case but often better due to pruning, and space complexity is O(h) for recursion stack.
Walk through a small BST and range to verify correctness, including edge cases like no nodes in range or all nodes in range.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.