Skip to content

Commit

Permalink
added Kth-smallest-element-in-a-bst problem
Browse files Browse the repository at this point in the history
  • Loading branch information
mukul96 committed Nov 3, 2022
1 parent 6c2d065 commit 9acb90e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 25 deletions.
29 changes: 29 additions & 0 deletions Binary Search Tree/Kth-smallest-element-in-a-bst.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
void inorder(TreeNode * root, int &currentIndex,int k, int &value){
if(root){
inorder(root->left, currentIndex, k, value);
currentIndex = currentIndex+1;
if(currentIndex==k){
value = root->val;
}
inorder(root->right, currentIndex, k, value);
}
}
int kthSmallest(TreeNode* root, int k) {
int currentIndex = 0, value = -1;
inorder(root, currentIndex, k, value);
return value;
}
};
25 changes: 0 additions & 25 deletions Binary Search/Untitled-1

This file was deleted.

0 comments on commit 9acb90e

Please sign in to comment.