Minimum Absolute Difference in BST (by LeetCode) and AMP!!!

Hello my dears,

  It has been a while since I've posted something here, so here we go. Today we'll be landing on LeetCode territory again, and we'll deal with Binary Search Trees and In-Order Traversal. But first, some news: Bing has shipped AMP (Accelerated Mobile Pages) on m.bing.com. Yeahhh! We had done this work before on the Bing iOS and Android native apps, but now we're rolling it out to the browser (m.bing.com), so whenever you search for any query that triggers a News article, sure enough you shall see the famous AMP icon there, and the pages will load much faster than the non-AMP ones. Very cool project done by my team:


Now back to LeetCode. The problem in question is this: https://leetcode.com/problems/minimum-absolute-difference-in-bst/#/description, or copied/pasted here:

Given a binary search tree with non-negative values, find the minimum absolute difference between values of any two nodes.
Example:
Input:

   1
    \
     3
    /
   2

Output:
1

Explanation:
The minimum absolute difference is 1, which is the difference between 2 and 1 (or between 2 and 3).
Note: There are at least two nodes in this BST.
It is important to use the only crucial information in this problem: that we're dealing with a Binary-Search-Tree (BST). With that information in mind, remember that an In-Order traversal of the tree will give you all its elements in sorted order. If your tree for instance is:
  6
3  7
The In-Order traversal will give you 3,6,7. Now with that it becomes very easy to solve it:
a) Do an In-Order traversal passing in the previous value
b) The processing will consist of checking <current value> - <previous value> and keeping tracking of the min
Linear time, very fast as the response indicates. Code is down below - cheers! Marcelo

    public class Solution
    {
        public int GetMinimumDifference(TreeNode root)
        {
            int minDiff = Int32.MaxValue;
            int previousValue = -1;
            GetMinDiffInOrder(root, ref previousValue, ref minDiff);
            return minDiff;
        }

        private void GetMinDiffInOrder(TreeNode currentNode,
                                       ref int previousValue,
                                       ref int minDiff)
        {
            if (currentNode == null) return;

            GetMinDiffInOrder(currentNode.left, ref previousValue, ref minDiff);
            if (previousValue >= 0 && currentNode.val - previousValue < minDiff) minDiff = currentNode.val - previousValue;
            previousValue = currentNode.val;
            GetMinDiffInOrder(currentNode.right, ref previousValue, ref minDiff);
        }
    }

Comments

  1. Congrats on your launch Marcelo and awesome to see some new posts from you! This time I have nothing to remove, so according to Antoine de Saint-Exupery you've achieved perfection :)

    Just for completeness adding my C++ solution:
    class Solution {
    public:
    int getMinimumDifference(const TreeNode* root) {
    int previous = -1;
    int min_so_far = numeric_limits::max();
    function traverse = [&](const TreeNode* current) {
    if (current->left) traverse(current->left);
    if (previous != -1) {
    min_so_far = min(min_so_far, current->val - previous);
    }
    previous = current->val;
    if (current->right) traverse(current->right);
    };
    traverse(root);
    return min_so_far;
    }
    };

    Have a fabulous weekend and thanks for posting!

    ReplyDelete

Post a Comment

Popular posts from this blog

Changing the root of a binary tree

Prompt Engineering and LeetCode

ProjectEuler Problem 719 (some hints, but no spoilers)