Skip to content

Instantly share code, notes, and snippets.

@sourabh2k15
Created December 26, 2017 01:36
Show Gist options
  • Save sourabh2k15/a25308e4dbfd0f91f1164aea1f63598f to your computer and use it in GitHub Desktop.
Save sourabh2k15/a25308e4dbfd0f91f1164aea1f63598f to your computer and use it in GitHub Desktop.
Leetcode #230 Find Kth Smallest element in BST
class Solution {
public:
int kthSmallest(TreeNode* root, int k) {
stack<TreeNode*> s;
while(root != NULL || !s.empty()){
while(root != NULL){
s.push(root);
root = root->left;
}
root = s.top(); s.pop();
if(--k == 0) return root->val;
root = root->right;
}
return -1;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment