0
0
DSA C++programming~20 mins

Validate if Tree is BST in DSA C++ - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
BST Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of BST Validation on a Simple Tree
What is the output of the following C++ code that checks if a binary tree is a BST?
DSA C++
struct Node {
    int val;
    Node* left;
    Node* right;
    Node(int x) : val(x), left(nullptr), right(nullptr) {}
};

bool isBSTUtil(Node* root, int minVal, int maxVal) {
    if (!root) return true;
    if (root->val <= minVal || root->val >= maxVal) return false;
    return isBSTUtil(root->left, minVal, root->val) && isBSTUtil(root->right, root->val, maxVal);
}

bool isBST(Node* root) {
    return isBSTUtil(root, INT_MIN, INT_MAX);
}

int main() {
    Node* root = new Node(10);
    root->left = new Node(5);
    root->right = new Node(15);
    root->left->left = new Node(2);
    root->left->right = new Node(7);
    root->right->left = new Node(12);
    root->right->right = new Node(20);
    
    if (isBST(root))
        std::cout << "true" << std::endl;
    else
        std::cout << "false" << std::endl;
    return 0;
}
Atrue
Bfalse
CCompilation error
DRuntime error
Attempts:
2 left
💡 Hint
Check if all nodes follow the BST property: left < root < right.
Predict Output
intermediate
2:00remaining
Output when Tree Violates BST Property
What is the output of this C++ code that checks if a binary tree is a BST, where one node violates the BST property?
DSA C++
struct Node {
    int val;
    Node* left;
    Node* right;
    Node(int x) : val(x), left(nullptr), right(nullptr) {}
};

bool isBSTUtil(Node* root, int minVal, int maxVal) {
    if (!root) return true;
    if (root->val <= minVal || root->val >= maxVal) return false;
    return isBSTUtil(root->left, minVal, root->val) && isBSTUtil(root->right, root->val, maxVal);
}

bool isBST(Node* root) {
    return isBSTUtil(root, INT_MIN, INT_MAX);
}

int main() {
    Node* root = new Node(10);
    root->left = new Node(5);
    root->right = new Node(8); // Violates BST property: should be > 10
    root->left->left = new Node(2);
    root->left->right = new Node(7);
    
    if (isBST(root))
        std::cout << "true" << std::endl;
    else
        std::cout << "false" << std::endl;
    return 0;
}
ACompilation error
Btrue
Cfalse
DRuntime error
Attempts:
2 left
💡 Hint
Check the right child of root; it must be greater than root's value.
🧠 Conceptual
advanced
1:30remaining
Why Use Min and Max Bounds in BST Validation?
Why does the BST validation function pass minimum and maximum values down the recursion?
ATo count the number of nodes in the tree.
BTo ensure each node's value is within the valid range defined by its ancestors.
CTo balance the tree during traversal.
DTo store the path from root to current node.
Attempts:
2 left
💡 Hint
Think about how BST property depends on ancestor nodes, not just immediate parent.
🔧 Debug
advanced
2:00remaining
Identify the Bug in BST Validation Code
What is the bug in this BST validation code snippet?
DSA C++
bool isBSTUtil(Node* root, int minVal, int maxVal) {
    if (!root) return true;
    if (root->val < minVal || root->val > maxVal) return false;
    return isBSTUtil(root->left, minVal, root->val) && isBSTUtil(root->right, root->val, maxVal);
}
AThe comparison should use <= and >= to handle equal values correctly.
BThe function should not use recursion.
CThe base case should return false instead of true.
DThe minVal and maxVal parameters are not needed.
Attempts:
2 left
💡 Hint
Consider if equal values are allowed in BST and how the code handles them.
🚀 Application
expert
2:30remaining
Number of Nodes in a Valid BST After Insertions
Given an initially empty BST, the following values are inserted in order: 20, 10, 30, 25, 40, 22. After all insertions, how many nodes does the BST contain?
A4
B5
C7
D6
Attempts:
2 left
💡 Hint
Count each inserted value as one node; no duplicates are inserted.