Finding minimum absolute difference within a Binary Search Tree in JavaScript

We are required to write a JavaScript function that takes in the root of a BST that holds some numerical data like this:

    1
     \
      3
     /
    2

The function should return the minimum absolute difference between any two nodes of the tree.

For the above tree, the output should be:

const output = 1;

because |1 - 2| = |3 - 2| = 1

Understanding the Approach

The key insight is that in a Binary Search Tree, an in-order traversal visits nodes in ascending order. Therefore, the minimum difference will always be between two consecutive nodes in this sorted sequence.

Example Implementation

class Node {
    constructor(data) {
        this.data = data;
        this.left = null;
        this.right = null;
    }
}

class BinarySearchTree {
    constructor() {
        this.root = null;
    }
    
    insert(data) {
        var newNode = new Node(data);
        if (this.root === null) {
            this.root = newNode;
        } else {
            this.insertNode(this.root, newNode);
        }
    }
    
    insertNode(node, newNode) {
        if (newNode.data < node.data) {
            if (node.left === null) {
                node.left = newNode;
            } else {
                this.insertNode(node.left, newNode);
            }
        } else {
            if (node.right === null) {
                node.right = newNode;
            } else {
                this.insertNode(node.right, newNode);
            }
        }
    }
}

const BST = new BinarySearchTree();
BST.insert(1);
BST.insert(3);
BST.insert(2);

const getMinimumDifference = function(root) {
    const nodes = [];
    
    // In-order traversal to get sorted values
    const dfs = (root) => {
        if (root) {
            dfs(root.left);
            nodes.push(root.data);
            dfs(root.right);
        }
    };
    
    dfs(root);
    
    // Find minimum difference between consecutive nodes
    let result = nodes[1] - nodes[0];
    for (let i = 1; i < nodes.length - 1; i++) {
        result = Math.min(result, nodes[i + 1] - nodes[i]);
    }
    
    return result;
};

console.log(getMinimumDifference(BST.root));

Output

1

How It Works

The algorithm works in two phases:

  1. In-order Traversal: Visit nodes in left-root-right order, which gives us values in ascending order for a BST
  2. Find Minimum Difference: Compare consecutive values in the sorted array to find the smallest difference

Optimized Single-Pass Solution

We can optimize this to avoid storing all nodes by tracking the previous value during traversal:

const getMinimumDifferenceOptimized = function(root) {
    let minDiff = Infinity;
    let prevValue = null;
    
    const inOrder = (node) => {
        if (!node) return;
        
        inOrder(node.left);
        
        if (prevValue !== null) {
            minDiff = Math.min(minDiff, node.data - prevValue);
        }
        prevValue = node.data;
        
        inOrder(node.right);
    };
    
    inOrder(root);
    return minDiff;
};

// Test with the same BST
console.log(getMinimumDifferenceOptimized(BST.root));
1

Time and Space Complexity

Approach Time Complexity Space Complexity
Two-pass (with array) O(n) O(n)
Single-pass (optimized) O(n) O(h) where h is tree height

Conclusion

Finding the minimum absolute difference in a BST leverages the in-order traversal property. The optimized single-pass approach is more space-efficient while maintaining the same time complexity.

Updated on: 2026-03-15T23:19:00+05:30

263 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements