LeetCode 530. Minimum Absolute Difference in BST(在二叉查找树中查找两个节点之差的最小绝对值)

Posted SomnusMistletoe

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode 530. Minimum Absolute Difference in BST(在二叉查找树中查找两个节点之差的最小绝对值)相关的知识,希望对你有一定的参考价值。

题意:在二叉查找树中查找两个节点之差的最小绝对值

/**
 * 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 ans = 0x3f3f3f3f;
    TreeNode* pre = NULL;
    void inorder(TreeNode* root){
        if(root == NULL) return;
        inorder(root -> left);
        if(pre) ans = min(ans, root -> val - pre -> val);
        pre = root;
        inorder(root -> right);
    }
    int getMinimumDifference(TreeNode* root) {
        inorder(root);
        return ans;
    }
};

  

以上是关于LeetCode 530. Minimum Absolute Difference in BST(在二叉查找树中查找两个节点之差的最小绝对值)的主要内容,如果未能解决你的问题,请参考以下文章

530. Minimum Absolute Difference in BST(LeetCode)

[LeetCode] 530. Minimum Absolute Difference in BST Java

[LeetCode&Python] Problem 530. Minimum Absolute Difference in BST

[leetcode]Binary Search Tree-530. Minimum Absolute Difference in BST

LeetCode 530. Minimum Absolute Difference in BST(在二叉查找树中查找两个节点之差的最小绝对值)

530. Minimum Absolute Difference in BST