0
0
DSA C++programming~10 mins

Kth Smallest Element in BST in DSA C++ - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare the function that finds the kth smallest element in a BST.

DSA C++
int kthSmallest(TreeNode* root, int k) {
    int count = 0;
    int result = 0;
    inorder(root, k, count, [1]);
    return result;
}
Drag options to blanks, or click blank then click option'
Acount
Broot
Ck
Dresult
Attempts:
3 left
💡 Hint
Common Mistakes
Passing count instead of result by reference.
Not passing any variable by reference.
2fill in blank
medium

Complete the inorder traversal code to find the kth smallest element.

DSA C++
void inorder(TreeNode* root, int k, int& count, int& result) {
    if (!root) return;
    inorder(root->left, k, count, result);
    count++;
    if (count == [1]) {
        result = root->val;
        return;
    }
    inorder(root->right, k, count, result);
}
Drag options to blanks, or click blank then click option'
Ak
Bcount
Cresult
Droot->val
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing count with result instead of k.
Using root->val in the condition.
3fill in blank
hard

Fix the error in the base case of the inorder traversal function.

DSA C++
void inorder(TreeNode* root, int k, int& count, int& result) {
    if (root == [1]) return;
    inorder(root->left, k, count, result);
    count++;
    if (count == k) {
        result = root->val;
        return;
    }
    inorder(root->right, k, count, result);
}
Drag options to blanks, or click blank then click option'
Anullptr
Bfalse
CNULL
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 or NULL instead of nullptr.
Using false which is a boolean, not a pointer.
4fill in blank
hard

Fill both blanks to complete the iterative inorder traversal to find the kth smallest element.

DSA C++
int kthSmallest(TreeNode* root, int k) {
    std::stack<TreeNode*> stack;
    TreeNode* current = root;
    int count = 0;
    while (current != [1] || !stack.[2]()) {
        while (current != nullptr) {
            stack.push(current);
            current = current->left;
        }
        current = stack.top();
        stack.pop();
        count++;
        if (count == k) return current->val;
        current = current->right;
    }
    return -1;
}
Drag options to blanks, or click blank then click option'
Anullptr
Bempty
Csize
Dtop
Attempts:
3 left
💡 Hint
Common Mistakes
Using stack.size() instead of stack.empty().
Using NULL instead of nullptr.
5fill in blank
hard

Fill all three blanks to complete the recursive helper function signature and call.

DSA C++
void [1](TreeNode* root, int k, int& count, int& result) {
    if (root == nullptr) return;
    [1](root->left, k, count, result);
    count++;
    if (count == k) {
        result = root->val;
        return;
    }
    [1](root->right, k, count, result);
}

int kthSmallest(TreeNode* root, int k) {
    int count = 0;
    int result = 0;
    [2](root, k, count, result);
    return result;
}
Drag options to blanks, or click blank then click option'
Ainorder
BinorderTraversal
CkthSmallestHelper
Dtraverse
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for the helper function in definition and calls.
Using the main function name as helper.