Skip to content

Commit 9550e8c

Browse files
committed
Kth Smallest solution
1 parent 3b7f9a9 commit 9550e8c

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public:
3+
void search(TreeNode* root, int k, int& cnt, int& result){
4+
if(root == NULL)
5+
return;
6+
if(cnt > k)
7+
return;
8+
9+
search(root->left, k, cnt, result);
10+
11+
cnt++;
12+
13+
if(cnt == k)
14+
result = root->val;
15+
16+
search(root->right, k, cnt, result);
17+
}
18+
19+
int kthSmallest(TreeNode* root, int k) {
20+
int cnt = 0;
21+
int result = 0;
22+
23+
search(root, k, cnt, result);
24+
25+
return result;
26+
}
27+
};

0 commit comments

Comments
 (0)