Using Inorder Traversal (BST Property) – O(n) Time and O(n) Space
The idea is to take advantage of the Binary Search Tree property where inorder traversal gives values in sorted order. Once the values are sorted, the minimum absolute difference will always be found between adjacent. So instead of comparing all pairs, we only check consecutive elements, which significantly reduces the time complexity.
Perform inorder traversal to store node values in sorted order
Traverse the sorted list and compute difference between adjacent elements
Track the minimum difference while iterating
Return the minimum difference obtained
Output
1
Using Inorder Traversal (Without Extra Space) – O(n) Time and O(h) Space
The idea is to use inorder traversal and keep track of the previously visited node during traversal. Since the values are visited in sorted order, the minimum difference will always occur between consecutive nodes.
Perform inorder traversal and maintain a pointer to the previously visited node
At each node, compute the difference with the previous node
Update the minimum difference accordingly
Continue traversal and return the final minimum difference