https://leetcode.cn/problems/kth-smallest-element-in-a-bst/description/
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
void preorder(TreeNode *root, vector<int> &res)
{
if(root == nullptr)
{
return;
}
res.push_back(root->val);
preorder(root->left, res);
preorder(root->right, res);
}
int kthSmallest(TreeNode* root, int k)
{
vector<int> res;
preorder(root, res);
sort(res.begin(), res.end());
return res[k - 1];
}
};