File: range-sum-of-bst/solution.py

Date: 2026-06-06

Time: 18:37

range-sum-of-bst/solution.py

Purpose

This file is the self-contained solution and test suite for LeetCode 938 — Range Sum of BST. It owns the problem's data structure (TreeNode), the solving algorithm (rangesumbst), a tree-building helper, and all unit tests. Like every other problem directory in this repo, it follows the pattern of a single solution.py that can be run standalone via unittest.

Key Components

TreeNode — Standard BST node with val, left, right. Defined locally rather than imported from a shared module, making the file self-contained. Uses Optional[TreeNode] with from _future_ import annotations to allow the forward reference in the type hint.

rangesumbst(root, low, high) -> int — The core algorithm. Given a BST root and an inclusive range [low, high], returns the sum of all node values within that range. It exploits BST ordering to prune:

buildtree(values) -> Optional[TreeNode] — Test utility that constructs a tree from a level-order list (BFS serialization), where None entries represent missing children. Uses a queue to assign children left-to-right at each level.

TestRangeSumBST — Nine test cases covering: LeetCode examples, single-node in/out of range, all/none in range, empty tree, left-skewed tree, and boundary-value inclusion.

Patterns

BST-aware pruning — Rather than visiting every node (which would be O(n)), the algorithm skips entire subtrees that can't contain in-range values. This is the canonical approach for BST range queries and brings the complexity down toward O(h + k) where h is tree height and k is the number of in-range nodes.

Recursive DFS — The solution uses pure recursion with no explicit stack. The base case (not root) returns the additive identity 0, and the three branches cleanly partition all cases. No mutable state.

Self-contained problem moduleTreeNode is redefined locally in every tree problem rather than shared, matching LeetCode's submission format where the class is provided in-scope.

Dependencies

Imports: Only stdlib — annotations (for self-referential type hints), unittest, typing.Optional. No external packages.

Imported by: The testsolution.py file in this same directory, plus the massive list of testsolution.py files across ~450 other problem directories. That "imported by" list is misleading — those files don't actually import rangesumbst. They likely share the same test runner infrastructure or the dependency graph is showing co-membership in the test suite run by run_tests.py at the repo root.

Flow

1. Caller passes a BST root and [low, high] bounds.

2. At each node, one of four things happens:

3. Recursion unwinds, accumulating the sum via return values.

Invariants

Error Handling

None. The function trusts its inputs: root is either None or a valid TreeNode, and low <= high. There are no exceptions, no input validation, no sentinel error values. This is appropriate for a LeetCode solution where inputs are guaranteed by the judge.

Topics to Explore

Beliefs