0
0
DSA C++programming~20 mins

Kth Smallest Element in BST in DSA C++ - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Kth Smallest Element Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Inorder Traversal for Kth Smallest
What is the output of the inorder traversal of this BST before finding the 3rd smallest element?
DSA C++
struct Node {
    int val;
    Node* left;
    Node* right;
    Node(int x) : val(x), left(nullptr), right(nullptr) {}
};

Node* root = new Node(5);
root->left = new Node(3);
root->right = new Node(7);
root->left->left = new Node(2);
root->left->right = new Node(4);
root->right->left = new Node(6);
root->right->right = new Node(8);

void inorder(Node* node, vector<int>& res) {
    if (!node) return;
    inorder(node->left, res);
    res.push_back(node->val);
    inorder(node->right, res);
}

vector<int> result;
inorder(root, result);
for (int v : result) cout << v << " ";
A2 3 4 5 6 7 8
B8 7 6 5 4 3 2
C5 3 2 4 7 6 8
D3 2 4 5 7 6 8
Attempts:
2 left
💡 Hint
Inorder traversal of BST visits nodes in ascending order.
Predict Output
intermediate
2:00remaining
Output of Kth Smallest Element Function
What is the output of the function that finds the 4th smallest element in the BST below?
DSA C++
struct Node {
    int val;
    Node* left;
    Node* right;
    Node(int x) : val(x), left(nullptr), right(nullptr) {}
};

int count = 0;
int kthSmallest = -1;

void inorderKth(Node* node, int k) {
    if (!node || count >= k) return;
    inorderKth(node->left, k);
    count++;
    if (count == k) {
        kthSmallest = node->val;
        return;
    }
    inorderKth(node->right, k);
}

Node* root = new Node(5);
root->left = new Node(3);
root->right = new Node(7);
root->left->left = new Node(2);
root->left->right = new Node(4);
root->right->left = new Node(6);
root->right->right = new Node(8);

inorderKth(root, 4);
cout << kthSmallest;
A3
B5
C6
D4
Attempts:
2 left
💡 Hint
Count nodes visited in ascending order until reaching k.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Kth Smallest Element Code
What error will this code produce when trying to find the 2nd smallest element?
DSA C++
struct Node {
    int val;
    Node* left;
    Node* right;
    Node(int x) : val(x), left(nullptr), right(nullptr) {}
};

int count = 0;
int kthSmallest = -1;

void inorderKth(Node* node, int k) {
    if (!node) return;
    inorderKth(node->left, k);
    count++;
    if (count == k) {
        kthSmallest = node->val;
    }
    inorderKth(node->right, k);
}

Node* root = new Node(3);
root->left = new Node(1);
root->right = new Node(4);
root->left->right = new Node(2);

inorderKth(root, 2);
cout << kthSmallest;
ARuns infinitely
BOutputs 2 correctly
COutputs -1 (default value)
DOutputs 3 instead of 2
Attempts:
2 left
💡 Hint
Check if traversal stops after finding kth element.
🧠 Conceptual
advanced
1:30remaining
Time Complexity of Kth Smallest Element in BST
What is the average time complexity to find the kth smallest element in a balanced BST using inorder traversal?
AO(k)
BO(n)
CO(log n)
DO(k log n)
Attempts:
2 left
💡 Hint
Inorder traversal visits nodes in ascending order until kth is found.
🚀 Application
expert
2:30remaining
Result of Kth Smallest Element After Tree Modification
Given the BST below, after inserting a new node with value 1, what is the 3rd smallest element?
DSA C++
struct Node {
    int val;
    Node* left;
    Node* right;
    Node(int x) : val(x), left(nullptr), right(nullptr) {}
};

Node* root = new Node(5);
root->left = new Node(3);
root->right = new Node(7);
root->left->left = new Node(2);
root->left->right = new Node(4);
root->right->left = new Node(6);
root->right->right = new Node(8);

// Insert new node with value 1
root->left->left->left = new Node(1);

int count = 0;
int kthSmallest = -1;

void inorderKth(Node* node, int k) {
    if (!node || count >= k) return;
    inorderKth(node->left, k);
    count++;
    if (count == k) {
        kthSmallest = node->val;
        return;
    }
    inorderKth(node->right, k);
}

inorderKth(root, 3);
cout << kthSmallest;
A1
B3
C2
D4
Attempts:
2 left
💡 Hint
Remember to include the new node in the sorted order.