Related Articles
Pair with minimum absolute difference | BST
• Difficulty Level : Medium
• Last Updated : 30 Aug, 2019

Given a binary search tree of size N > 1, the task is to find the minimum absolute difference between any two nodes.

Examples:

```Input:
5
/   \
3     7
/ \   / \
2   4 6   8
Output: 1
Difference between all the consecutive nodes if sorted is 1.
Thus, the answer is 1.

Input:
1
\
6
Output: 5
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: We know that in-order traversal of a Binary Search Tree traverses it in sorted order. So, for every node, we will find its difference from the previous node in the in-order traversal of the tree. If this difference is smaller than the previous minimum difference, we will update the previous minimum difference. Following are the steps to follow:

1. Create a variable ‘prev’ to store the pointer to the previous node in in-order traversal.
2. Create a variable ‘ans’ to store the minimum difference.
3. For every node in the in-order traversal, compare its absolute difference with the previous node and update the minimum absolute difference found so far.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ``#include  ``using` `namespace` `std; ``   ` `// Node of the binary tree ``struct` `node { ``    ``int` `data; ``    ``node* left; ``    ``node* right; ``    ``node(``int` `data) ``    ``{ ``        ``this``->data = data; ``        ``left = NULL; ``        ``right = NULL; ``    ``} ``}; ``   ` `// Function for in-order traversal of the tree ``void` `inorder(node* curr, node*& prev, ``int``& ans) ``{ ``   ` `    ``// Base-case ``    ``if` `(curr == NULL) ``        ``return``; ``   ` `    ``// Calling in-order on the left sub-tree ``    ``inorder(curr->left, prev, ans); ``   ` `    ``if` `(prev != NULL) ``        ``ans = min(curr->data - prev->data, ans); ``    ``prev = curr; ``   ` `    ``// Calling in-order on the right sub-tree ``    ``inorder(curr->right, prev, ans); ``} ``   ` `// Function to return the minimum ``// difference between any two nodes ``// of the given binary search tree ``int` `minDiff(node* root) ``{ ``   ` `    ``// Pointer to previous node in the ``    ``// in-order traversal of the BST ``    ``node* prev = NULL; ``   ` `    ``// To store the final ans ``    ``int` `ans = INT_MAX; ``   ` `    ``// Call in-order for the BST ``    ``inorder(root, prev, ans); ``   ` `    ``// Returning the final answer ``    ``return` `ans; ``} ``   ` `// Driver code ``int` `main() ``{ ``    ``node* root = ``new` `node(5); ``    ``root->left = ``new` `node(3); ``    ``root->right = ``new` `node(7); ``    ``root->left->left = ``new` `node(2); ``    ``root->left->right = ``new` `node(4); ``    ``root->right->left = ``new` `node(6); ``    ``root->right->right = ``new` `node(8); ``   ` `    ``cout << minDiff(root); ``   ` `    ``return` `0; ``}`

## Java

 `// Java implementation of the approach ``import` `java.util.*;`` ` `class` `GFG ``{``     ` `// Node of the binary tree ``static` `class` `node ``{ ``    ``int` `data; ``    ``node left; ``    ``node right; ``    ``node(``int` `data) ``    ``{ ``        ``this``.data = data; ``        ``left = ``null``; ``        ``right = ``null``; ``    ``} ``}; ``static` `node prev;``static` `int` `ans;`` ` `// Function for in-order traversal of the tree ``static` `void` `inorder(node curr) ``{ ``     ` `    ``// Base-case ``    ``if` `(curr == ``null``) ``        ``return``; ``     ` `    ``// Calling in-order on the left sub-tree ``    ``inorder(curr.left); ``     ` `    ``if` `(prev != ``null``) ``        ``ans = Math.min(curr.data - ``                       ``prev.data, ans); ``    ``prev = curr; ``     ` `    ``// Calling in-order on the right sub-tree ``    ``inorder(curr.right); ``} ``     ` `// Function to return the minimum ``// difference between any two nodes ``// of the given binary search tree ``static` `int` `minDiff(node root) ``{ ``     ` `    ``// Pointer to previous node in the ``    ``// in-order traversal of the BST ``    ``prev = ``null``; ``     ` `    ``// To store the final ans ``    ``ans = Integer.MAX_VALUE; ``     ` `    ``// Call in-order for the BST ``    ``inorder(root); ``     ` `    ``// Returning the final answer ``    ``return` `ans; ``} ``     ` `// Driver code ``public` `static` `void` `main(String[] args)``{``    ``node root = ``new` `node(``5``); ``    ``root.left = ``new` `node(``3``); ``    ``root.right = ``new` `node(``7``); ``    ``root.left.left = ``new` `node(``2``); ``    ``root.left.right = ``new` `node(``4``); ``    ``root.right.left = ``new` `node(``6``); ``    ``root.right.right = ``new` `node(``8``); ``     ` `    ``System.out.println(minDiff(root)); ``}``}`` ` `// This code is contributed by 29AjayKumar`

## C#

 `// C# implementation of the approach``using` `System;``     ` `class` `GFG ``{``     ` `// Node of the binary tree ``public` `class` `node ``{ ``    ``public` `int` `data; ``    ``public` `node left; ``    ``public` `node right; ``    ``public` `node(``int` `data) ``    ``{ ``        ``this``.data = data; ``        ``left = ``null``; ``        ``right = ``null``; ``    ``} ``}; ``static` `node prev;``static` `int` `ans;`` ` `// Function for in-order traversal of the tree ``static` `void` `inorder(node curr) ``{ ``     ` `    ``// Base-case ``    ``if` `(curr == ``null``) ``        ``return``; ``     ` `    ``// Calling in-order on the left sub-tree ``    ``inorder(curr.left); ``     ` `    ``if` `(prev != ``null``) ``        ``ans = Math.Min(curr.data - ``                       ``prev.data, ans); ``    ``prev = curr; ``     ` `    ``// Calling in-order on the right sub-tree ``    ``inorder(curr.right); ``} ``     ` `// Function to return the minimum ``// difference between any two nodes ``// of the given binary search tree ``static` `int` `minDiff(node root) ``{ ``     ` `    ``// Pointer to previous node in the ``    ``// in-order traversal of the BST ``    ``prev = ``null``; ``     ` `    ``// To store the final ans ``    ``ans = ``int``.MaxValue; ``     ` `    ``// Call in-order for the BST ``    ``inorder(root); ``     ` `    ``// Returning the final answer ``    ``return` `ans; ``} ``     ` `// Driver code ``public` `static` `void` `Main(String[] args)``{``    ``node root = ``new` `node(5); ``    ``root.left = ``new` `node(3); ``    ``root.right = ``new` `node(7); ``    ``root.left.left = ``new` `node(2); ``    ``root.left.right = ``new` `node(4); ``    ``root.right.left = ``new` `node(6); ``    ``root.right.right = ``new` `node(8); ``     ` `    ``Console.WriteLine(minDiff(root)); ``}``}`` ` `// This code is contributed by PrinciRaj1992`

Another Approach with O(1) Space Complexity:

 `# Python 3 implementation of the approach`` ` `# Node of the binary tree``import` `math``class` `Node:  ``   ` `    ``# Constructor to create a new node  ``    ``def` `__init__(``self``, data):  ``        ``self``.data ``=` `data  ``        ``self``.left ``=` `None``        ``self``.right ``=` `None``#Set the target to infinity``target``=``math.inf  ``#Function to find the minimum absolute difference``def` `absolute_diff(root,target):``    ``if` `root ``is` `None``:``        ``return` `target``    ` `    ``if` `root.left ``is` `not` `None``:``        ``left``=``root.data``-``root.left.data``        ``target``=``min``(target,left)``    ``if` `root.right ``is` `not` `None``:``        ``right``=``root.right.data``-``root.data``        ``target``=``min``(target,right)``    ``#Find the minimum in the left subtree``    ``p``=``absolute_diff(root.left,target)``    ``#Find the minimum in the right subtree``    ``q``=``absolute_diff(root.right,target)``    ``return` `min``(p,q)`` ` `/``/` `Driver code``root``=``Node(``5``)``root.left ``=`  `Node(``3``)``root.right ``=` `Node(``7``)``root.left.left ``=`  `Node(``2``)``root.left.right ``=`  `Node(``4``)``root.right.left ``=`  `Node(``6``)``root.right.right ``=` `Node(``8``)``print``(absolute_diff(root,target))`
Output:
```1
```

Time complexity: O(N)
Additional Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up