思路 DFS+优先队列
BFS应该也可以
/**
* 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 {
private:
priority_queue<int, vector<int>, greater<int>> pq;
public:
void DFS(TreeNode* root) {
pq.push(root->val);
if (root->left == NULL && root->right == NULL) return;
if (root->left != NULL) DFS(root->left);
if (root->right != NULL) DFS(root->right);
}
int kthSmallest(TreeNode* root, int k) {
DFS(root);
while (-- k) {
pq.pop();
}
return pq.top();
}
};