0
0
DSA C++programming~10 mins

BST Property and Why It Matters 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 insert a value into a BST correctly.

DSA C++
if (value < root->val) {
    root->left = insert(root->left, [1]);
} else {
    root->right = insert(root->right, value);
}
Drag options to blanks, or click blank then click option'
Avalue
Broot->val
Croot
Dnullptr
Attempts:
3 left
💡 Hint
Common Mistakes
Using root->val instead of value causes infinite recursion.
Using nullptr will cause errors because insert expects a value.
2fill in blank
medium

Complete the condition to check if a BST node's left child is valid.

DSA C++
if (root->left && root->left->val [1] root->val) {
    // valid left child
}
Drag options to blanks, or click blank then click option'
A>=
B>
C==
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using > instead of < reverses the BST property.
Using == allows duplicates on the left, which is usually not allowed.
3fill in blank
hard

Fix the error in the BST search function to return true when the value matches the current node.

DSA C++
if (root == nullptr) return false;
if (root->val [1] value) return true;
if (value < root->val) return search(root->left, value);
else return search(root->right, value);
Drag options to blanks, or click blank then click option'
A==
B!=
C<
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using != causes the function to return true incorrectly.
Using < or > here breaks the logic of finding the exact value.
4fill in blank
hard

Fill both blanks to correctly check BST property for right child.

DSA C++
if (root->right && root->right->val [1] root->val) {
    return false; // violates BST property
}
return root->left == nullptr || root->left->val [2] root->val;
Drag options to blanks, or click blank then click option'
A<=
B>
C<
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using <= or >= allows duplicates which may violate strict BST rules.
Mixing up the comparison operators for left and right children.
5fill in blank
hard

Fill all three blanks to create a correct BST validation condition.

DSA C++
bool isValidBST(TreeNode* root) {
    if (!root) return true;
    if (root->left && root->left->val [1] root->val) return false;
    if (root->right && root->right->val [2] root->val) return false;
    return isValidBST(root->left) && isValidBST(root->right) && root->val [3] INT_MAX;
}
Drag options to blanks, or click blank then click option'
A<
B>
C<=
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using <= or >= incorrectly for child comparisons.
Not checking the root value against INT_MAX properly.