easy530. Minimum Absolute Difference in BST

Posted Sherry_Yang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了easy530. 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
    }
};

 

以上是关于easy530. Minimum Absolute Difference in BST的主要内容,如果未能解决你的问题,请参考以下文章

530. Minimum Absolute Difference in BST

[leetcode-530-Minimum Absolute Difference in BST]

Leetcode 530. Minimum Absolute Difference in BST

530. Minimum Absolute Difference in BST(LeetCode)

530. Minimum Absolute Difference in BST

[LeetCode] 530. Minimum Absolute Difference in BST Java