Minimum Diff with a Given Value in BST

Last Updated : 30 Jul, 2026

Given the root of a Binary Search Tree (BST) and an integer k, find the minimum absolute difference between k and the value of any node in the BST.

Examples:

Input :  root = [10, 2, 11, 1, 5, N, N, N, N, 3, 6, N, 4], k = 13

bst1

Output:  2
Explanation: The node that has value nearest to k is 11. So, the minimum absolute difference is |11-13| = 2.

Input : root = [8, 1, 9, N, 4, N, 10, 3], k = 9

blobid1_1783744852

Output:  0
Explanation: The node that has value nearest to k is 9. So, the minimum absolute difference is |9-9| = 0.

Try It Yourself
redirect icon

Complete Traversal - O(n) Time and O(h) Space

The idea is to traverse every node of the Binary Search Tree (BST) and calculate the absolute difference between the current node's value and k. During the traversal, recursively compute the minimum difference for the left and right subtrees, and return the smallest among the current node, left subtree, and right subtree. Since every node is visited exactly once, the minimum absolute difference with k is obtained.

C++
#include <iostream>
#include <climits>
#include <cmath>
using namespace std;

// Structure of a tree node
struct Node {
    int data;
    Node* left;
    Node* right;

    Node(int val) {
        data = val;
        left = right = nullptr;
    }
};

// Function to find least absolute difference
int minDiff(Node* root, int k) {
    if (root == nullptr)
        return INT_MAX;

    // Diff with root
    int diff = abs(root->data - k);

    // Return the minimum of three values: root,
    // minimum in left and right subtrees
    return min(diff, min(minDiff(root->left, k),
                         minDiff(root->right, k)));
}

int main() {

    // Create the BST
    //          10
    //         /  \
    //        2    11
    //       / \
    //      1   5
    //         / \
    //        3   6
    //         \
    //          4

    Node* root = new Node(10);
    root->left = new Node(2);
    root->right = new Node(11);
    root->left->left = new Node(1);
    root->left->right = new Node(5);
    root->left->right->left = new Node(3);
    root->left->right->right = new Node(6);
    root->left->right->left->right = new Node(4);

    int k = 13;

    cout << minDiff(root, k);

    return 0;
}
Java
// Structure of a tree node
class Node {
    int data;
    Node left;
    Node right;

    Node(int val) {
        data = val;
        left = right = null;
    }
}

class GFG {

    // Function to find least absolute difference
    static int minDiff(Node root, int k) {
        if (root == null)
            return Integer.MAX_VALUE;

        // Diff with root
        int diff = Math.abs(root.data - k);

        // Return the minimum of three values: root,
        // minimum in left and right subtrees
        return Math.min(diff, Math.min(minDiff(root.left, k),
                                       minDiff(root.right, k)));
    }

    public static void main(String[] args) {

        // Create the BST
        //          10
        //         /  \
        //        2    11
        //       / \
        //      1   5
        //         / \
        //        3   6
        //         \
        //          4

        Node root = new Node(10);
        root.left = new Node(2);
        root.right = new Node(11);
        root.left.left = new Node(1);
        root.left.right = new Node(5);
        root.left.right.left = new Node(3);
        root.left.right.right = new Node(6);
        root.left.right.left.right = new Node(4);

        int k = 13;

        System.out.println(minDiff(root, k));
    }
}
Python
# Structure of a tree node
class Node:
    def __init__(self, val):
        self.data = val
        self.left = None
        self.right = None


# Function to find least absolute difference
def minDiff(root, k):
    if root is None:
        return float('inf')

    # Diff with root
    diff = abs(root.data - k)

    # Return the minimum of three values: root,
    # minimum in left and right subtrees
    return min(diff,
               min(minDiff(root.left, k),
                   minDiff(root.right, k)))


if __name__ == "__main__":

    # Create the BST
    #          10
    #         /  \
    #        2    11
    #       / \
    #      1   5
    #         / \
    #        3   6
    #         \
    #          4

    root = Node(10)
    root.left = Node(2)
    root.right = Node(11)
    root.left.left = Node(1)
    root.left.right = Node(5)
    root.left.right.left = Node(3)
    root.left.right.right = Node(6)
    root.left.right.left.right = Node(4)

    k = 13

    print(minDiff(root, k))
C#
using System;

// Structure of a tree node
class Node {
    public int data;
    public Node left;
    public Node right;

    public Node(int val) {
        data = val;
        left = right = null;
    }
}

class GFG {

    // Function to find least absolute difference
    static int minDiff(Node root, int k) {
        if (root == null)
            return int.MaxValue;

        // Diff with root
        int diff = Math.Abs(root.data - k);

        // Return the minimum of three values: root,
        // minimum in left and right subtrees
        return Math.Min(diff, Math.Min(minDiff(root.left, k),
                                       minDiff(root.right, k)));
    }

    static void Main() {

        // Create the BST
        //          10
        //         /  \
        //        2    11
        //       / \
        //      1   5
        //         / \
        //        3   6
        //         \
        //          4

        Node root = new Node(10);
        root.left = new Node(2);
        root.right = new Node(11);
        root.left.left = new Node(1);
        root.left.right = new Node(5);
        root.left.right.left = new Node(3);
        root.left.right.right = new Node(6);
        root.left.right.left.right = new Node(4);

        int k = 13;

        Console.WriteLine(minDiff(root, k));
    }
}
JavaScript
// Structure of a tree node
class Node {
    constructor(val) {
        this.data = val;
        this.left = null;
        this.right = null;
    }
}

// Function to find least absolute difference
function minDiff(root, k) {
    if (root === null)
        return Number.MAX_SAFE_INTEGER;

    // Diff with root
    let diff = Math.abs(root.data - k);

    // Return the minimum of three values: root,
    // minimum in left and right subtrees
    return Math.min(diff,
                    Math.min(minDiff(root.left, k),
                             minDiff(root.right, k)));
}

// Driver code

// Create the BST
//          10
//         /  \
//        2    11
//       / \
//      1   5
//         / \
//        3   6
//         \
//          4

let root = new Node(10);
root.left = new Node(2);
root.right = new Node(11);
root.left.left = new Node(1);
root.left.right = new Node(5);
root.left.right.left = new Node(3);
root.left.right.right = new Node(6);
root.left.right.left.right = new Node(4);

let k = 13;

console.log(minDiff(root, k));

Output
2

Using BST Property - O(h) Time and O(1) Space

The idea is to use the Binary Search Tree (BST) property to avoid visiting every node. While traversing the tree, keep track of the minimum absolute difference found so far. If the current node value is greater than k, move to the left subtree; otherwise, move to the right subtree, as it may contain a value closer to k.

  • Initialize a variable res with a very large value.
  • Start traversing the BST from the root.
  • At each node, update res with the minimum of its current value and abs(current->data - k).
  • If the current node value is greater than k, move to the left child.
  • Otherwise, move to the right child.
  • Continue until the current node becomes NULL.
  • Return res.
C++
#include <iostream>
using namespace std;

class Node
{
  public:
    int data;
    Node *left;
    Node *right;

    Node(int x)
    {
        data = x;
        left = right = nullptr;
    }
};

// Find minimum absolute difference with k
int minDiff(Node *root, int k)
{
    int res = INT_MAX;
    Node *current = root;

    while (current != nullptr) {
        
        // Update answer
        res = min(res, abs(current->data - k));

        // Move according to BST property
        if (current->data > k) {
            current = current->left;
        } else {
            current = current->right;
        }
    }

    return res;
}
int main() {

    // Create the BST
    //          10
    //         /  \
    //        2    11
    //       / \
    //      1   5
    //         / \
    //        3   6
    //         \
    //          4

    Node* root = new Node(10);
    root->left = new Node(2);
    root->right = new Node(11);
    root->left->left = new Node(1);
    root->left->right = new Node(5);
    root->left->right->left = new Node(3);
    root->left->right->right = new Node(6);
    root->left->right->left->right = new Node(4);

    int k = 13;

    cout << minDiff(root, k);

    return 0;
}
Java
class Node {
    int data;
    Node left;
    Node right;

    Node(int x) {
        data = x;
        left = right = null;
    }
}

class GFG {

    // Find minimum absolute difference with k
    static int minDiff(Node root, int k) {
        int res = Integer.MAX_VALUE;
        Node current = root;

        while (current != null) {

            // Update answer
            res = Math.min(res, Math.abs(current.data - k));

            // Move according to BST property
            if (current.data > k) {
                current = current.left;
            } else {
                current = current.right;
            }
        }

        return res;
    }

    public static void main(String[] args) {

        // Create the BST
        //          10
        //         /  \
        //        2    11
        //       / \
        //      1   5
        //         / \
        //        3   6
        //         \
        //          4

        Node root = new Node(10);
        root.left = new Node(2);
        root.right = new Node(11);
        root.left.left = new Node(1);
        root.left.right = new Node(5);
        root.left.right.left = new Node(3);
        root.left.right.right = new Node(6);
        root.left.right.left.right = new Node(4);

        int k = 13;

        System.out.println(minDiff(root, k));
    }
}
Python
class Node:
    def __init__(self, x):
        self.data = x
        self.left = None
        self.right = None


# Find minimum absolute difference with k
def minDiff(root, k):
    res = float('inf')
    current = root

    while current is not None:

        # Update answer
        res = min(res, abs(current.data - k))

        # Move according to BST property
        if current.data > k:
            current = current.left
        else:
            current = current.right

    return res


if __name__ == "__main__":

    # Create the BST
    #          10
    #         /  \
    #        2    11
    #       / \
    #      1   5
    #         / \
    #        3   6
    #         \
    #          4

    root = Node(10)
    root.left = Node(2)
    root.right = Node(11)
    root.left.left = Node(1)
    root.left.right = Node(5)
    root.left.right.left = Node(3)
    root.left.right.right = Node(6)
    root.left.right.left.right = Node(4)

    k = 13

    print(minDiff(root, k))
C#
using System;

class Node {
    public int data;
    public Node left;
    public Node right;

    public Node(int x) {
        data = x;
        left = right = null;
    }
}

class GFG {

    // Find minimum absolute difference with k
    static int minDiff(Node root, int k) {
        int res = int.MaxValue;
        Node current = root;

        while (current != null) {

            // Update answer
            res = Math.Min(res, Math.Abs(current.data - k));

            // Move according to BST property
            if (current.data > k) {
                current = current.left;
            } else {
                current = current.right;
            }
        }

        return res;
    }

    static void Main() {

        // Create the BST
        //          10
        //         /  \
        //        2    11
        //       / \
        //      1   5
        //         / \
        //        3   6
        //         \
        //          4

        Node root = new Node(10);
        root.left = new Node(2);
        root.right = new Node(11);
        root.left.left = new Node(1);
        root.left.right = new Node(5);
        root.left.right.left = new Node(3);
        root.left.right.right = new Node(6);
        root.left.right.left.right = new Node(4);

        int k = 13;

        Console.WriteLine(minDiff(root, k));
    }
}
JavaScript
class Node {
    constructor(x) {
        this.data = x;
        this.left = null;
        this.right = null;
    }
}

// Find minimum absolute difference with k
function minDiff(root, k) {
    let res = Number.MAX_SAFE_INTEGER;
    let current = root;

    while (current !== null) {

        // Update answer
        res = Math.min(res, Math.abs(current.data - k));

        // Move according to BST property
        if (current.data > k) {
            current = current.left;
        } else {
            current = current.right;
        }
    }

    return res;
}

// Driver code

// Create the BST
//          10
//         /  \
//        2    11
//       / \
//      1   5
//         / \
//        3   6
//         \
//          4

let root = new Node(10);
root.left = new Node(2);
root.right = new Node(11);
root.left.left = new Node(1);
root.left.right = new Node(5);
root.left.right.left = new Node(3);
root.left.right.right = new Node(6);
root.left.right.left.right = new Node(4);

let k = 13;

console.log(minDiff(root, k));

Output
2
Comment