真的不会编😰
Given a binary search tree, you are supposed to find the node that contains the K-th largest key.
Format of function:
BinTree KthLargest ( BinTree T, int K );
where BinTree is defined as the following:
typedef struct TNode *BinTree;
struct TNode{
int Key;
BinTree Left;
BinTree Right;
};
The function KthLargest is supposed to return the pointer that points to the node that contains the K-th largest key in the binary search tree T.
Here T is not empty and all its keys are distinct positive integers. K is positive and is never more than the total number of nodes in the tree.
Sample program of judge:
#include <stdio.h>
#include <stdlib.h>
typedef struct TNode *BinTree;
struct TNode{
int Key;
BinTree Left;
BinTree Right;
};
BinTree BuildTree(); /* details omitted */
BinTree KthLargest ( BinTree T, int K );
int main()
{
BinTree T, P;
int K;
T = BuildTree();
scanf("%d", &K);
P = KthLargest(T, K);
printf("%d\n", P->Key);
if (P->Left) printf("%d\n", P->Left->Key);
else printf("NULL\n");
if (P->Right) printf("%d\n", P->Right->Key);
else printf("NULL\n");
return 0;
}
/* Your function will be put here */
Sample Input: (for the following tree)
4
Sample Output:
5
NULL
7