---
# System prepended metadata

title: 'LeetCode #230 [Kth Smallest Element in a BST](https://leetcode.com/problems/kth-smallest-element-in-a-bst/)'
tags: [Recursion, LeetCode, Medium, Tree]

---

###### tags: `LeetCode` `Recursion` `Tree` `Medium`
# LeetCode #230 [Kth Smallest Element in a BST](https://leetcode.com/problems/kth-smallest-element-in-a-bst/)
### (Medium)

```
/**
 * 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:
    vector<int> v;
    int kthSmallest(TreeNode* root, int k) {
        helper(root);
        sort(v.begin(), v.end());
        return v[k-1];
    }
    void helper(TreeNode* node){
        if(!node)return;
        v.push_back(node->val);
        helper(node->left);
        helper(node->right);
    }
};
```