-
Notifications
You must be signed in to change notification settings - Fork 0
/
kLargestInBST.c
44 lines (36 loc) · 905 Bytes
/
kLargestInBST.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <stdio.h>
#include <stdlib.h>
typedef struct elementT {
struct elementT *left;
struct elementT *right;
int data;
} element;
void getKLargestValue(element *root, int k) {
int num = 0;
getKLargestValueSub(root, k, &num);
}
void getKLargestValueSub(element *root, int k, int *num) {
if(!root)
return;
getKLargestValueSub(root->right, k, num);
(*num)++;
if(*num == k)
printf("%d\n", root->data);
getKLargestValueSub(root->left, k, num);
}
element *makeNode(int d) {
element *ele = (element *)malloc(sizeof(element));
ele->data = d;
return ele;
}
main()
{
element *root = makeNode(50);
root->left = makeNode(25);
root->right = makeNode(75);
root->right->left = makeNode(60);
root->right->right = makeNode(80);
getKLargestValue(root, 1);
getKLargestValue(root, 5);
getKLargestValue(root, 3);
}