【leetcode】二叉搜索树节点最小距离

int cmp(const void* a, const void* b){
    return *(int*)a - *(int*)b;
}
void func(struct TreeNode* root,int* arr,int* pst)
{
    if (!root) return ;
    arr[(*pst)++] = root->val;
    func(root->left,arr,pst);
    func(root->right,arr,pst);
}

int minDiffInBST(struct TreeNode* root){
    if (!root) return NULL;
    int arr[101] = {0};
    int pst=0,min=100,i;
    func(root,arr,&pst);
    qsort(arr,pst,sizeof(int),cmp);
    for (i=0; i<pst-1; i++)
    {
        if (abs(arr[i]-arr[i+1]) < min) min = abs(arr[i]-arr[i+1]);
    }
    return min;    
}
原文地址:https://www.cnblogs.com/ganxiang/p/13689645.html