```
class Solution {
public:
int kthSmallest(TreeNode* root, int k) {
int res;
tree_nodes(root, k, res);
return res;
}
private:
void tree_nodes(TreeNode* root, int &k, int &res){
if(!root || k <= 0)
return;
// check left children
tree_nodes(root->left, k, res);
if(k <= 0)
return;
else if(k == 1){
res = root->val;
--k;
return;
}else{
--k;
tree_nodes(root->right, k, res);
}
}
};
```