0
0
DSA C++programming~20 mins

BST Property and Why It Matters in DSA C++ - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
BST Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Inorder Traversal on a BST
What is the output of the inorder traversal of this BST after inserting the values 10, 5, 15, 3, 7, 12, 18 in that order?
DSA C++
struct Node {
    int val;
    Node* left;
    Node* right;
    Node(int x) : val(x), left(nullptr), right(nullptr) {}
};

Node* insert(Node* root, int val) {
    if (!root) return new Node(val);
    if (val < root->val) root->left = insert(root->left, val);
    else root->right = insert(root->right, val);
    return root;
}

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

int main() {
    Node* root = nullptr;
    int values[] = {10, 5, 15, 3, 7, 12, 18};
    for (int v : values) root = insert(root, v);
    std::vector<int> result;
    inorder(root, result);
    for (int x : result) std::cout << x << " ";
    return 0;
}
A[5, 3, 7, 10, 15, 12, 18]
B[10, 5, 3, 7, 15, 12, 18]
C[3, 5, 7, 10, 12, 15, 18]
D[18, 15, 12, 10, 7, 5, 3]
Attempts:
2 left
💡 Hint
Inorder traversal of a BST always gives sorted values.
🧠 Conceptual
intermediate
1:30remaining
Why BST Property is Important for Search Efficiency
Why does the BST property help in efficient searching compared to an unsorted binary tree?
ABecause it balances the tree automatically without extra operations.
BBecause it allows skipping half of the tree at each step by comparing values.
CBecause it stores all values in a linked list inside each node.
DBecause it stores values in random order to confuse the search.
Attempts:
2 left
💡 Hint
Think about how comparing values guides the search direction.
🔧 Debug
advanced
2:00remaining
Identify the Bug in BST Insertion
What error will this BST insertion code cause when inserting duplicate values?
DSA C++
Node* insert(Node* root, int val) {
    if (!root) return new Node(val);
    if (val < root->val) root->left = insert(root->left, val);
    else if (val > root->val) root->right = insert(root->right, val);
    return root;
}
ADuplicates are inserted to the right subtree always.
BDuplicates cause infinite recursion leading to stack overflow.
CDuplicates are inserted to the left subtree always.
DDuplicates are ignored; no insertion happens for equal values.
Attempts:
2 left
💡 Hint
Check what happens when val == root->val.
Predict Output
advanced
2:30remaining
Output After Deleting a Node with Two Children in BST
Given the BST constructed by inserting 20, 10, 30, 5, 15, 25, 35, what is the inorder traversal output after deleting node 20?
DSA C++
struct Node {
    int val;
    Node* left;
    Node* right;
    Node(int x) : val(x), left(nullptr), right(nullptr) {}
};

Node* insert(Node* root, int val) {
    if (!root) return new Node(val);
    if (val < root->val) root->left = insert(root->left, val);
    else root->right = insert(root->right, val);
    return root;
}

Node* findMin(Node* root) {
    while (root && root->left) root = root->left;
    return root;
}

Node* deleteNode(Node* root, int val) {
    if (!root) return nullptr;
    if (val < root->val) root->left = deleteNode(root->left, val);
    else if (val > root->val) root->right = deleteNode(root->right, val);
    else {
        if (!root->left) {
            Node* temp = root->right;
            delete root;
            return temp;
        } else if (!root->right) {
            Node* temp = root->left;
            delete root;
            return temp;
        }
        Node* temp = findMin(root->right);
        root->val = temp->val;
        root->right = deleteNode(root->right, temp->val);
    }
    return root;
}

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

int main() {
    Node* root = nullptr;
    int values[] = {20, 10, 30, 5, 15, 25, 35};
    for (int v : values) root = insert(root, v);
    root = deleteNode(root, 20);
    std::vector<int> result;
    inorder(root, result);
    for (int x : result) std::cout << x << " ";
    return 0;
}
A[5, 10, 15, 25, 30, 35]
B[5, 10, 15, 20, 25, 30, 35]
C[5, 10, 15, 25, 20, 30, 35]
D[10, 15, 25, 30, 35]
Attempts:
2 left
💡 Hint
Deleting a node with two children replaces it with the smallest node in the right subtree.
🧠 Conceptual
expert
1:30remaining
Effect of Violating BST Property on Search
If a binary tree does not maintain the BST property, what is the worst-case time complexity of searching for a value compared to a BST?
AIt becomes O(n) because the tree may behave like a linked list.
BIt remains O(log n) because the tree structure is unchanged.
CIt improves to O(1) because values are randomly distributed.
DIt becomes O(n log n) due to extra comparisons.
Attempts:
2 left
💡 Hint
Without BST property, you cannot decide which subtree to skip.