class Solution {
public:
int minDiffInBST(TreeNode* root) {
stack<TreeNode*> stk;
int pre = -1, ans = INT_MAX;
while (!stk.empty() || root != nullptr) {
while (root != nullptr) {
stk.push(root);
root = root->left;
}
root = stk.top();
stk.pop();
if (pre != -1)
ans = min(root->val - pre, ans);
pre = root->val;
root = root->right;
}
return ans;
}
};