0
0
DSA C++programming~20 mins

BST Insert Operation in DSA C++ - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
BST Insert Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output after inserting nodes in BST
What is the printed state of the BST after inserting the values 10, 5, 15, 3, 7 in that order? The BST is printed using in-order traversal (left -> root -> right).
DSA C++
struct Node {
    int data;
    Node* left;
    Node* right;
    Node(int val) : data(val), left(nullptr), right(nullptr) {}
};

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

void inorder(Node* root) {
    if (!root) return;
    inorder(root->left);
    std::cout << root->data << " -> ";
    inorder(root->right);
}

int main() {
    Node* root = nullptr;
    root = insert(root, 10);
    root = insert(root, 5);
    root = insert(root, 15);
    root = insert(root, 3);
    root = insert(root, 7);
    inorder(root);
    std::cout << "null" << std::endl;
    return 0;
}
A3 -> 5 -> 7 -> 10 -> 15 -> null
B10 -> 5 -> 15 -> 3 -> 7 -> null
C15 -> 10 -> 7 -> 5 -> 3 -> null
D3 -> 7 -> 5 -> 10 -> 15 -> null
Attempts:
2 left
💡 Hint
Remember, in-order traversal prints nodes in ascending order for BST.
Predict Output
intermediate
2:00remaining
Output after inserting duplicate value in BST
What is the printed state of the BST after inserting the values 20, 10, 30, 20 in that order? Assume duplicates are inserted to the right subtree. The BST is printed using in-order traversal.
DSA C++
struct Node {
    int data;
    Node* left;
    Node* right;
    Node(int val) : data(val), left(nullptr), right(nullptr) {}
};

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

void inorder(Node* root) {
    if (!root) return;
    inorder(root->left);
    std::cout << root->data << " -> ";
    inorder(root->right);
}

int main() {
    Node* root = nullptr;
    root = insert(root, 20);
    root = insert(root, 10);
    root = insert(root, 30);
    root = insert(root, 20);
    inorder(root);
    std::cout << "null" << std::endl;
    return 0;
}
A10 -> 30 -> 20 -> 20 -> null
B10 -> 20 -> 30 -> 20 -> null
C20 -> 10 -> 30 -> 20 -> null
D10 -> 20 -> 20 -> 30 -> null
Attempts:
2 left
💡 Hint
Duplicates go to the right subtree, so the second 20 is after the first 20.
🔧 Debug
advanced
2:00remaining
Identify the error in BST insert function
What error will this BST insert function cause when inserting values? Explain the output or error.
DSA C++
struct Node {
    int data;
    Node* left;
    Node* right;
    Node(int val) : data(val), left(nullptr), right(nullptr) {}
};

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

void inorder(Node* root) {
    if (!root) return;
    inorder(root->left);
    std::cout << root->data << " -> ";
    inorder(root->right);
}

int main() {
    Node* root = nullptr;
    root = insert(root, 10);
    root = insert(root, 10);
    root = insert(root, 5);
    root = insert(root, 15);
    // Print in-order
    inorder(root);
    std::cout << "null" << std::endl;
    return 0;
}
ACompilation error due to missing inorder function
B5 -> 10 -> 10 -> 15 -> null
C5 -> 10 -> 15 -> null
DStack overflow due to infinite recursion
Attempts:
2 left
💡 Hint
Check how duplicates are handled in the insert function.
Predict Output
advanced
2:00remaining
Output after inserting nodes and printing pre-order traversal
What is the printed state of the BST after inserting 8, 3, 10, 1, 6, 14, 4, 7, 13 in that order? The BST is printed using pre-order traversal (root -> left -> right).
DSA C++
struct Node {
    int data;
    Node* left;
    Node* right;
    Node(int val) : data(val), left(nullptr), right(nullptr) {}
};

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

void preorder(Node* root) {
    if (!root) return;
    std::cout << root->data << " -> ";
    preorder(root->left);
    preorder(root->right);
}

int main() {
    Node* root = nullptr;
    int values[] = {8, 3, 10, 1, 6, 14, 4, 7, 13};
    for (int val : values) root = insert(root, val);
    preorder(root);
    std::cout << "null" << std::endl;
    return 0;
}
A1 -> 3 -> 4 -> 6 -> 7 -> 8 -> 10 -> 13 -> 14 -> null
B8 -> 10 -> 14 -> 13 -> 3 -> 6 -> 7 -> 4 -> 1 -> null
C8 -> 3 -> 1 -> 6 -> 4 -> 7 -> 10 -> 14 -> 13 -> null
D3 -> 1 -> 6 -> 4 -> 7 -> 8 -> 10 -> 14 -> 13 -> null
Attempts:
2 left
💡 Hint
Pre-order prints root first, then left subtree, then right subtree.
🧠 Conceptual
expert
2:00remaining
Number of nodes in BST after insertions
After inserting the values 50, 30, 70, 20, 40, 60, 80, 70, 30 into an empty BST (duplicates inserted to the right), how many nodes does the BST contain?
A9
B8
C10
D7
Attempts:
2 left
💡 Hint
Count unique values plus duplicates inserted as new nodes.