```
class Solution {
public:
int kth(TreeNode* root, int k, int& n){
int val = 0;
if(root->left) val = kth(root->left, k, n);
if(n == k) return val;
if(++n == k) return root->val;
if(root->right) val = kth(root->right, k, n);
return val;
}
int kthSmallest(TreeNode* root, int k) {
if(NULL == root) return 0;
int n = 0;
return kth(root, k, n);
}
};
```