【leetcode】二叉搜索树的第k大节点

int count;
void fun(struct TreeNode* node,int* arr)
{
    if (!node)
        return;
    arr[count++] = node->val;
    fun(node->left,arr);
    fun(node->right,arr);
}
int Mycmp(const void* a,const void* b)
{
    return *(int*)b - *(int*)a;
}
int kthLargest(struct TreeNode* root, int k){
    if (!root)
    {
        return NULL;
    }
    count = 0;
    int arr[10000];
    arr[count++] = root->val;
    fun(root->left,arr);
    fun(root->right,arr);

    qsort(arr,count,sizeof(int),Mycmp);

    for (int i=0; i<count; i++)
    {
        if (k == 1)
        {
            return arr[i];
        }
        if (arr[i] != arr[i+1])
            k--;
    }
    return arr[0];
}
原文地址:https://www.cnblogs.com/ganxiang/p/13553610.html