Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
✗ Incorrect
We must insert the same value into the left subtree if it is less than the current node's value.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using > instead of < reverses the BST property.
Using == allows duplicates on the left, which is usually not allowed.
✗ Incorrect
In a BST, the left child's value must be less than the current node's value.
3fill in blank
hardFix 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'
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.
✗ Incorrect
We return true when the current node's value equals the search value.
4fill in blank
hardFill 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'
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.
✗ Incorrect
Right child's value must be greater than the current node's value; left child's value must be less.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using <= or >= incorrectly for child comparisons.
Not checking the root value against INT_MAX properly.
✗ Incorrect
Left child must be less than root, right child greater than root, and root value must be less than or equal to INT_MAX.