class Solution {
public:
int ans;
int step;
stack<TreeNode*> s;
Solution(){
step = 0;
}
int kthSmallest(TreeNode* root, int k) {
TreeNode *p = root;
while(p || !s.empty()){
while(p){
s.push(p);
p = p->left;
}
if(!s.empty()){
p = s.top();
s.pop();
++step;
if(step == k)
return p->val;
p = p->right;
}
}
return 0;
}
};
230. Kth Smallest Element in a BST
最新推荐文章于 2021-04-09 14:18:22 发布