/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int minDiffInBST(TreeNode* root) {
int minVal = INT_MAX;
int val = -1;
inOrder(root, val, minVal);
return minVal;
}
private:
void inOrder(TreeNode* root, int& val, int& minVal) {
if (!root) {
return;
}
inOrder(root->left, val, minVal);
if (val > 0) {
minVal = min(minVal, abs(root->val - val));
}
val = root->val;
inOrder(root->right, val, minVal);
}
};