/**
* 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) {
if (root == NULL) return -1;
int retVal = -1;
int count = 0;
findIt(root, k, count, retVal);
return retVal;
}
void findIt(TreeNode* node, int k, int& count, int& ret) {
if (count >= k) return;
if (node->left != NULL) findIt(node->left, k, count, ret);
count++;
if (count == k) {
ret = node->val;
return;
}
if (node->right != NULL) findIt(node->right, k, count, ret);
}
};