File: minimum-absolute-difference-in-bst/solution.py

Date: 2026-06-06

Time: 17:51

Purpose

This file is a self-contained LeetCode solution for LeetCode 530: Minimum Absolute Difference in BST. It owns three responsibilities: the TreeNode definition, the Solution class with the algorithm, and inline unit tests. It follows the same solution.py + test_solution.py convention used across the ~400+ problem directories in this repo, though here the tests are bundled into the solution file rather than separated.

Key Components

TreeNode

Standard binary tree node with val, left, right. Used as the input structure — matches the LeetCode definition.

Solution.getMinimumDifference(root) -> int

The core algorithm. Finds the minimum absolute difference between any two node values in a BST. Exploits the BST property: an inorder traversal visits nodes in ascending order, so the minimum difference must occur between consecutive elements in that traversal.

Contract: root has at least 2 nodes and forms a valid BST. Returns a non-negative integer.

build_tree(values) -> TreeNode | None

Test helper that constructs a binary tree from a level-order list (BFS layout), where None entries represent missing children. Standard LeetCode tree serialization format.

TestMinAbsDiff

Six test cases covering: LeetCode examples, two-node edge case, right-skewed tree, mixed large/small gaps, and consecutive values.

Patterns

Inorder traversal with running state — The solution uses instance variables (self.prev, self.min_diff) as mutable state shared across recursive calls rather than passing accumulators through parameters or using a nonlocal closure. This is a common LeetCode idiom for BST problems where you need to compare consecutive inorder elements.

Implicit sorted-sequence reduction — Instead of materializing the full sorted list then scanning for min-diff (O(n) space), this computes the answer during traversal by only tracking the previous value (O(h) stack space, where h is tree height).

Dependencies

Imports: typing.Optional for type hints, unittest for tests. No external libraries.

Imported by: The "Imported By" list in the prompt is misleading — those are test files from *other* problems that likely import from their own solution.py, not from this file. The actual dependent is minimum-absolute-difference-in-bst/test_solution.py.

Flow

1. getMinimumDifference initializes self.prev = None and self.min_diff = inf

2. inorder(node) recurses left, processes current node, recurses right

3. On each node visit: if self.prev is set, compute node.val - self.prev (guaranteed non-negative because inorder on a BST yields ascending order) and update self.min_diff if smaller

4. Set self.prev = node.val for the next comparison

5. Return int(self.mindiff) — the int() cast converts from float('inf') type to int (though with ≥2 nodes, mindiff will always have been updated)

Invariants

Error Handling

None. The code trusts its input matches the LeetCode contract. No null-root guard beyond the recursive base case (if not node: return). The int() cast is the only defensive measure, converting the float sentinel to an integer return type.

Topics to Explore

Beliefs