0
0
DSA C++programming~10 mins

Validate if Tree is BST 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 check if the current node is null.

DSA C++
if (root == [1]) return true;
Drag options to blanks, or click blank then click option'
A0
Bnullptr
CNULL
Dfalse
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 or NULL instead of nullptr
Returning false instead of true here
2fill in blank
medium

Complete the code to check if the current node's value is within the allowed range.

DSA C++
if (root->val <= [1] || root->val >= maxVal) return false;
Drag options to blanks, or click blank then click option'
Aroot->val
BmaxVal
CminVal
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using maxVal instead of minVal here
Comparing with root->val itself
3fill in blank
hard

Fix the error in the recursive call to validate the left subtree.

DSA C++
return isValidBST(root->left, [1], root->val) && isValidBST(root->right, root->val, maxVal);
Drag options to blanks, or click blank then click option'
AminVal
Bnullptr
Croot->val
DmaxVal
Attempts:
3 left
💡 Hint
Common Mistakes
Using maxVal instead of minVal for left subtree
Passing nullptr instead of a value
4fill in blank
hard

Fill both blanks to complete the function signature and initial call for BST validation.

DSA C++
bool isValidBST(TreeNode* root, [1] minVal = [2], long maxVal = LONG_MAX) {
Drag options to blanks, or click blank then click option'
Along
Bint
Cnullptr
DLONG_MIN
Attempts:
3 left
💡 Hint
Common Mistakes
Using int instead of long
Using nullptr as default for minVal
5fill in blank
hard

Fill all three blanks to complete the recursive BST validation logic.

DSA C++
if (root == nullptr) return true;
if (root->val <= [1] || root->val >= [2]) return false;
return isValidBST(root->left, [1], root->val) && isValidBST(root->right, root->val, [2]);
Drag options to blanks, or click blank then click option'
AminVal
BmaxVal
Croot->val
Dnullptr
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up minVal and maxVal
Using root->val incorrectly in comparisons