Back to photostream

Minimum Absolute Difference in BST Leetcode Solution

The problem Minimum Absolute Difference in BST Leetcode Solution states that you are provided with a Binary Search Tree. And you are required to find the minimum absolute difference in the entire BST. A BST or a Binary Search Tree is nothing but a tree with some nodes that follow a rule. the rule is that the left subtree of a node must contain only the nodes that have a value less than the current node. Similarly, for the right subtree, the nodes must contain values greater than the current node. So, as usual, we must first take a look at a few examples before jumping into the solution.

 

1

Explanation: The difference between the nodes , , , all have difference of 1. And all other pairs have a greater difference. Thus the answer is 1.

Brute Force Approach for Minimum Absolute Difference in BST Leetcode Solution

The problem Minimum Absolute Difference in BST Leetcode Solution asked us to find the minimum difference between any two nodes in a given Binary Search Tree. So, one way to find the answer is to pick any two vertices or nodes of the BST and calculate the difference. We will update the answer once we find a pair that has a lesser absolute difference than the previous pair. Initially, we can use any two nodes, and at the end of the process. The answer will be stored in the respective variables. So, to simulate this process, we will traverse the BST in an inorder manner and store it in a vector.

 

www.tutorialcup.com/leetcode-solutions/minimum-absolute-d...

10 views
0 faves
0 comments
Uploaded on September 14, 2021