> For the complete documentation index, see [llms.txt](https://jenhsuan.gitbook.io/algorithm/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jenhsuan.gitbook.io/algorithm/leetcode/230.-kth-smallest-element-in-a-bst.md).

# 230. Kth Smallest Element in a BST

## 1.問題

* 找出第K小的element

![](/files/-LJd_VfbGBj8Cno40pVR)

## 2.想法

* 以inorder尋覽BST後會得到排序的list

## 3.程式碼

```
/**
 * 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) {
        vector<int> v;
        inOrder(root, v);
        
        return v[k - 1];
    }
private:
    void inOrder(TreeNode* root, vector<int>& v) {
        
        if (root != NULL)
        {
            inOrder(root->left, v);
            v.push_back(root->val);
        inOrder(root->right, v);
        }
        
    }
};
```

## 4.Performance

![](/files/-LJd_Don4LleHmRtkgiF)
