class Solution {
public:
TreeNode* target = nullptr;
void inorder(TreeNode* root, int& count, int k){
if(!root) return;
inorder(root->left, count, k);
count++;
if(count == k) {
target = root;
return;
}
inorder(root->right, count, k);
}
int kthSmallest(TreeNode* root, int k) {
int count = 0;
inorder(root, count, k);
return target->val;
}
};
LeetCode Hot100 230.二叉搜索树中第K小的元素
于 2024-08-18 11:25:00 首次发布