llllmz

导航

530. 二叉搜索树的最小绝对差c

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
void inorder(struct TreeNode* root,int* t,int* pre){
    if(!root) return;
    inorder(root->left,t,pre);
    int x=abs(root->val-*pre);
    if(x<*t) *t=x;
    *pre=root->val;
    inorder(root->right,t,pre);
}

int getMinimumDifference(struct TreeNode* root) {
    int t=INT_MAX;
    int pre=INT_MAX;
    inorder(root,&t,&pre);
    return t;
}

结果:

posted on 2024-03-06 15:02  神奇的萝卜丝  阅读(19)  评论(0)    收藏  举报