BSTMINDIFF - Editorial

Prerequisites :- Binary Search Trees, BT traversals.

Problem :- Given a Binary Search Tree (BST), return the minimum difference between the values of any two different nodes in the tree.

Solution Approach :- To find the minimum difference between the values of any two nodes in a BST, we can perform an inorder traversal of the tree. During the traversal, we keep track of the previous visited node’s value and update the minimum difference as we encounter each node. The minimum difference occurs when we compare adjacent nodes in the sorted order of a BST.

Time complexity: O(N), because the algorithm visits all the nodes in the given BST.

Space complexity: O(1), as no extra space is needed.