Loading content...
Given the root node of a Binary Search Tree (BST), compute and return the smallest absolute difference between the values of any two distinct nodes in the tree.
A Binary Search Tree is a tree data structure where for every node:
The absolute difference between two values a and b is defined as |a - b|, which equals a - b if a ≥ b, and b - a otherwise.
Your task is to examine all possible pairs of nodes in the tree and find the pair with the minimum absolute difference between their values.
root = [4,2,6,1,3]1The BST contains nodes with values 1, 2, 3, 4, and 6. When examining all pairs, the minimum absolute differences occur between adjacent values in sorted order: |2-1| = 1, |3-2| = 1, |4-3| = 1. Multiple pairs achieve the minimum value of 1.
root = [1,0,48,null,null,12,49]1The tree has values 0, 1, 12, 48, and 49. The smallest absolute difference is |1-0| = 1, which is the gap between the two smallest values in the tree.
root = [5,3,8]2The tree contains only three nodes: 3, 5, and 8. The differences are |5-3| = 2 and |8-5| = 3. Therefore, the minimum absolute difference is 2.
Constraints