The goal of this problem is to compute the smallest absolute difference between the values of any two nodes in a Binary Search Tree (BST). Because a BST is a tree where the left child is smaller and the right child is larger than the parent node, we can leverage its sorted nature for an efficient solution. The challenge lies in finding the closest pair of values without having to compare every pair explicitly.
For example, if a BST contains the values 2, 4, and 6, the differences are 2 (between 2 and 4) and 2 (between 4 and 6). The minimum of these is 2, which would be the correct result.
The most efficient way to solve this problem is by using an in-order traversal, which visits the nodes of a BST in ascending order. This allows us to compare each node only with its immediate predecessor, as the smallest absolute difference will always occur between two adjacent values in a sorted sequence.
As we perform the traversal, we keep track of the value of the previously visited node. For each node visited, we compute the absolute difference between the current node and the previous one. If this difference is smaller than the current minimum, we update our result. By the time the traversal is complete, we will have found the smallest such difference.
The time complexity of this approach is O(n), where n is the number of nodes in the BST. Each node is visited exactly once during the traversal. The space complexity is O(h), where h is the height of the tree. This space is used by the recursion stack and can be as small as O(log n) for a balanced tree or as large as O(n) for a completely skewed tree.
The problem assumes that the BST has at least two nodes, so there's no need to handle the case where the tree is empty or has only one node. However, it's important to initialize the minimum difference with a large value (such as infinity) and ensure the previous node tracker starts as None
so that the first comparison is skipped properly.
The “Minimum Absolute Difference in BST” problem is a great example of how in-order traversal can be leveraged for efficient range-based calculations in a BST. Rather than comparing every pair of nodes, we make use of the tree’s ordering properties to compare only adjacent values in sorted order. This strategy minimizes both time and space, making it a highly efficient solution that exemplifies the power of recursion and tree traversal patterns.