Smart idea!

I think you can use TreeNode to represent a lower bound or an upper bound.

Here's my C++ version:

class Solution {
private:
void preorder(TreeNode* curr, TreeNode* lb, TreeNode* ub, int& min_diff) {
if (curr == nullptr) return;
if (lb != nullptr) min_diff = min(min_diff, curr->val - lb->val);
if (ub != nullptr) min_diff = min(min_diff, ub->val - curr->val);
preorder(curr->left, lb, curr, min_diff);
preorder(curr->right, curr, ub, min_diff);
}
public:
int getMinimumDifference(TreeNode* root) {
int min_diff = INT_MAX;
preorder(root, nullptr, nullptr, min_diff);
return min_diff;
}
};