← Home

530. Minimum Absolute Difference in BST

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
two pointersC++Markdown
530

The trick here is to name the state correctly, then let the implementation follow. For 530. Minimum Absolute Difference in BST, the solution in this repository is mainly a two pointers solution.

Guide

What?

We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: two pointers.

Guide

When?

This is the kind of solution you want when the problem has structure hiding inside a messy-looking input. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are inOrder, getMinimumDifference.

Guide

Why?

The solution works because it narrows the problem until every update has a clear reason to exist.

  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Read the setup variables first.
  2. Follow the main loop or recursive helper next.
  3. Watch where invalid states get skipped.
  4. Check which value survives to the return statement.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01/**
02Given a binary search tree with non-negative values, find the minimum absolute difference between values of any two nodes.
03
04Example:
05
06Input:
07
08   1
09    \
10     3
11    /
12   2
13
14Output:
151
16
17Explanation:
18The minimum absolute difference is 1, which is the difference between 2 and 1 (or between 2 and 3).
19 
20
21Note: There are at least two nodes in this BST.
22**/
23
24//Runtime: 36 ms, faster than 22.95% of C++ online submissions for Minimum Absolute Difference in BST.
25//Memory Usage: 21.8 MB, less than 96.61% of C++ online submissions for Minimum Absolute Difference in BST.
26/**
27 * Definition for a binary tree node.
28 * struct TreeNode {
29 *     int val;
30 *     TreeNode *left;
31 *     TreeNode *right;
32 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
33 * };
34 */
35class Solution {
36public:
37    int last = INT_MIN/2;
38    int ans = INT_MAX;
39    
40    void inOrder(TreeNode* node){
41        if(node->left) inOrder(node->left);
42        ans = min(ans, node->val - last);
43        last = node->val;
44        if(node->right) inOrder(node->right);
45    }
46    int getMinimumDifference(TreeNode* root) {
47        inOrder(root);
48        return ans;
49    }
50};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.