中序遍历
/**
* 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) {
stack<TreeNode*> stk;
int count=0;
TreeNode* cur=root;
while(cur || !stk.empty()){
while(cur){
stk.push(cur);
cur=cur->left;
}
cur=stk.top();
stk.pop();
if(++count==k)
break;
cur=cur->right;
}
return cur->val;
}
};