1、采用二叉树的中序遍历,先把所有的左子树添加进去,再开始计数到k
三、代码
class Solution {
public:
int kthSmallest(TreeNode* root, int k) {
stack stack;
while (root != nullptr || stack.size() > 0) {
while (root != nullptr) {
stack.push(root);
root = root->left;
}
root = stack.top();
stack.pop();
--k;
if (k == 0) {
break;
}
root = root->right;
}
return root->val;
}
};



