Skip to content

Commit 3d97a9d

Browse files
authored
Add files via upload
1 parent 4aa0db7 commit 3d97a9d

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

Kth_Smallest_Element_in_BST.cpp

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

0 commit comments

Comments
 (0)