We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 3b7f9a9 commit 9550e8cCopy full SHA for 9550e8c
kth-smallest-element-in-a-bst/PDKhan.cpp
@@ -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
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