Skip to content

Commit 0f8bd12

Browse files
committed
"Kth Smallest Element in a BST"
1 parent 9e78d8b commit 0f8bd12

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ You can build all the files using `make` (Use MinGW GCC and GNU Make on Windows)
2424

2525
| | Problem | Solution |
2626
| --- | ------------------------------------------------------------ | ------------------ |
27+
| 230 | [Kth Smallest Element in a BST] | [C](src/230.c) |
2728
| 229 | [Majority Element II] | |
2829
| 228 | [Summary Ranges] | [C](src/228.c) |
2930
| 227 | [Basic Calculator II] | |
@@ -242,6 +243,7 @@ You can build all the files using `make` (Use MinGW GCC and GNU Make on Windows)
242243
[LeetCode algorithm problems]: https://leetcode.com/problemset/algorithms/
243244

244245

246+
[Kth Smallest Element in a BST]: https://leetcode.com/problems/kth-smallest-element-in-a-bst/
245247
[Majority Element II]: https://leetcode.com/problems/majority-element-ii/
246248
[Summary Ranges]: https://leetcode.com/problems/summary-ranges/
247249
[Basic Calculator II]: https://leetcode.com/problems/basic-calculator-ii/

src/230.c

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
struct TreeNode {
5+
int val;
6+
struct TreeNode *left;
7+
struct TreeNode *right;
8+
};
9+
10+
/** Modification of Morris in-order tree traversal */
11+
int kthSmallest0(struct TreeNode* root, int k) {
12+
if (root == NULL || k == 0) return -1;
13+
14+
struct TreeNode *cur = root;
15+
struct TreeNode **p = NULL;
16+
int i = 0;
17+
int ans = -1;
18+
19+
while (cur != NULL) {
20+
if (cur->left != NULL) {
21+
/* find predecessor node */
22+
p = &(cur->left);
23+
while (1) {
24+
if (*p == NULL) {
25+
if (i >= k) cur = cur->right; /* get to rightest node asap */
26+
else {
27+
*p = cur;
28+
cur = cur->left;
29+
}
30+
break;
31+
}
32+
33+
if (*p == cur) {
34+
if (++i == k) ans = cur->val; /* can't just return, have to recover tree */
35+
*p = NULL; /* time complexity changes from O(k) to O(n) */
36+
cur = cur->right;
37+
break;
38+
}
39+
p = &((*p)->right);
40+
}
41+
}
42+
else {
43+
if (++i == k) ans = cur->val;
44+
cur = cur->right;
45+
}
46+
}
47+
48+
return ans;
49+
}
50+
51+
/** Divide and conquer, just like quick sort */
52+
int getCount(struct TreeNode* root) {
53+
if (root == NULL) return 0;
54+
return getCount(root->left) + getCount(root->right) + 1;
55+
}
56+
57+
int kthSmallest1(struct TreeNode* root, int k) {
58+
if (root == NULL || k == 0) return -1;
59+
60+
int l = getCount(root->left); /* it takes O(n) */
61+
if (l == k - 1) return root->val;
62+
if (l < k)
63+
return kthSmallest1(root->right, k - l - 1);
64+
else
65+
return kthSmallest1(root->left, k);
66+
}
67+
68+
/** In-order traversal */
69+
int findHelper(struct TreeNode* root, int* k) {
70+
if (root == NULL || *k == 0) return -1;
71+
72+
int x = findHelper(root->left, k);
73+
if (*k == 0) return x;
74+
(*k)--;
75+
if (*k == 0) return root->val;
76+
return findHelper(root->right, k);
77+
}
78+
79+
int kthSmallest(struct TreeNode* root, int k) {
80+
return findHelper(root, &k);
81+
}
82+
83+
int main() {
84+
struct TreeNode *r = (struct TreeNode *)calloc(9, sizeof(struct TreeNode));
85+
struct TreeNode *p = r;
86+
87+
p->val = 6;
88+
p->left = r + 1;
89+
p->right = r + 2;
90+
91+
p = p->left;
92+
p->val = 2;
93+
p->left = r + 3;
94+
p->right = r + 4;
95+
96+
p = p->left;
97+
p->val = 1;
98+
99+
p = r + 4;
100+
p->val = 4;
101+
p->left = r + 5;
102+
p->right = r + 6;
103+
104+
p = r + 5;
105+
p->val = 3;
106+
107+
p = r + 6;
108+
p->val = 5;
109+
110+
p = r + 2;
111+
p->val = 7;
112+
p->right = r + 7;
113+
114+
p = p->right;
115+
p->val = 9;
116+
p->left = r + 8;
117+
118+
p = p->left;
119+
p->val = 8;
120+
121+
int i;
122+
for (i = 1; i <= 9; i++) {
123+
printf("%d\n", kthSmallest(r, i));
124+
}
125+
return 0;
126+
}

0 commit comments

Comments
 (0)