【easy】530. Minimum Absolute Difference in BST
找BST树中节点之间的最小差值。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
//BST树中序遍历就是递增的序列,相邻两个数的差值,找最小
class Solution {
public:
int min_res = INT_MAX;
TreeNode* pre;
int getMinimumDifference(TreeNode* root) {
helper(root);
return min_res;
}
void helper(TreeNode*root){
if (!root)
return;
helper(root->left); //1
if (pre)
min_res = min(min_res, abs(root->val - pre->val));
pre = root;
helper(root->right); //2
}
};