题目:
Given a binary search tree, write a function kthSmallest
to find the kth smallest element in it.
Note:
You may assume k is always valid, 1 ≤ k ≤ BST's total elements.
void f(vector<int> &re,TreeNode* root) {
if(root!=NULL){
f(re,root->left);
re.push_back(root->val);
f(re,root->right);
}
}
class Solution {
public:
int kthSmallest(TreeNode* root, int k) {
vector<int> re;
f(re,root);
return re[k-1];
}
};