Recursive and Heap Based Simple Solution


#1
void solve(TreeNode* A, int B, priority_queue<int> &max_h){
    if(A==NULL)
    return;
    max_h.push(A->val);
    if(max_h.size()>B)
        max_h.pop();
    solve(A->left,B,max_h);
    solve(A->right,B,max_h);
} 
int Solution::kthsmallest(TreeNode* A, int B) {
    priority_queue<int> max_h;
    solve(A,B,max_h);
    return max_h.top();
}