We have a binary search tree (BST) and a range defined by low and high. We need to find every node whose value falls within that range (inclusive on both ends) and return their sum.
This is a BST, not an arbitrary binary tree. In a BST, for every node, all values in its left subtree are smaller and all values in its right subtree are larger. That ordering lets us prune entire subtrees that cannot contain values in our range. If a node's value is below low, its left subtree holds only smaller values, none of which qualify. If a node's value is above high, its right subtree holds only larger values.
Number of nodes up to 2 * 10^4: An O(n) traversal of the whole tree is acceptable. We do not need a sub-linear solution.1 <= Node.val <= 10^5: All values are positive, and the maximum possible sum is 2 10^4 10^5 = 2 10^9. This is below the 32-bit signed integer limit of about 2.147 10^9, so a standard int accumulator does not overflow.Node.val are unique, so each node contributes at most once to the sum.Traverse the entire tree with DFS and, at each node, check whether its value falls within [low, high]. If it does, add it to a running sum. This ignores the BST property and treats the tree as a generic binary tree, which means it works on any binary tree, not only a BST.
low and high (inclusive).This visits every node, including subtrees that cannot contain in-range values. The next approach uses the BST ordering to skip those subtrees.
The BST property lets us skip subtrees instead of visiting them. For any node:
node.val < low, then this node and everything in its left subtree is below the range, so we recurse only into the right subtree.node.val > high, then this node and everything in its right subtree is above the range, so we recurse only into the left subtree.low <= node.val <= high, the node is in range. We add its value and recurse into both subtrees, since the left subtree may hold values at least low and the right subtree may hold values at most high.Pruning skips entire branches that cannot contribute to the sum. On a balanced BST with a narrow range, the number of visited nodes drops from O(n) toward O(log n + k), where k is the count of in-range values.
The BST invariant guarantees that every value in a node's left subtree is strictly less than the node's value, and every value in its right subtree is strictly greater. When a node's value is below low, every value in its left subtree is also below low, so none of them can be in range and the entire subtree is safe to skip. The reverse holds for a node above high: its whole right subtree is above high.
low, recurse only on the right child (left subtree is all too small).high, recurse only on the left child (right subtree is all too large).The recursion depth equals the tree height, so a deeply skewed tree can reach a recursion depth of n. The next approach applies the same pruning iteratively with an explicit queue, which keeps the stack depth constant.
The same pruning logic works iteratively with a queue (BFS). We process nodes from the queue, add a value when it is in range, and enqueue a child only when its subtree could contain in-range values.
The decision to enqueue mirrors the recursive pruning. We enqueue the left child only when node.val > low, because if node.val <= low the entire left subtree is at most node.val and below the range. We enqueue the right child only when node.val < high, by the symmetric argument. For the constraints here (up to 2 * 10^4 nodes) recursion would not overflow the stack, but the iterative form keeps stack depth constant regardless of tree shape.
low, enqueue the left child (it might have in-range values).high, enqueue the right child (it might have in-range values).