Medium
Kth Smallest Element in a BST — C++
Full explanation · Time O(max(h, k)) · Space O(min(h, k))
// Time: O(max(h, k))
// Space: O(min(h, k))
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int kthSmallest(TreeNode* root, int k) {
deque<TreeNode *> s;
TreeNode *cur = root;
int rank = 0;
while (!s.empty() || cur) {
if (cur) {
s.emplace_back(cur);
if (s.size() > k) {
s.pop_front();
}
cur = cur->left;
} else {
cur = s.back();
s.pop_back();
if (++rank == k) {
return cur->val;
}
cur = cur->right;
}
}
return INT_MIN;
}
};
// Time: O(max(h, k))
// Space: O(h)
class Solution2 {
public:
int kthSmallest(TreeNode* root, int k) {
stack<TreeNode *> s;
TreeNode *cur = root;
int rank = 0;
while (!s.empty() || cur) {
if (cur) {
s.emplace(cur);
cur = cur->left;
} else {
cur = s.top();
s.pop();
if (++rank == k) {
return cur->val;
}
cur = cur->right;
}
}
return INT_MIN;
}
};