📖 Scenario: You are working with a simple binary tree structure in C++. You want to check if this tree follows the rules of a Binary Search Tree (BST). A BST is a tree where for every node, all nodes in the left subtree have smaller values, and all nodes in the right subtree have larger values.
🎯 Goal: Build a program that creates a binary tree, sets up a helper function to check BST rules, and then prints whether the tree is a valid BST or not.
📋 What You'll Learn
Create a binary tree with exactly 3 nodes with values 10, 5, and 15
Add a helper function to check if the tree is a BST using recursion
Use the helper function to determine if the tree is a BST
Print "BST" if the tree is valid, otherwise print "Not BST"
💡 Why This Matters
🌍 Real World
Checking if a tree is a BST is important in databases and search algorithms where data needs to be organized for fast lookup.
💼 Career
Understanding BST validation helps in roles involving data structures, algorithm design, and software engineering where tree data is common.
Progress0 / 4 steps
1
Create the binary tree nodes
Create a struct called Node with an int data and two pointers left and right. Then create three nodes named root, leftChild, and rightChild with values 10, 5, and 15 respectively. Connect leftChild as the left child of root and rightChild as the right child of root.
DSA C++
Hint
Define the Node struct first. Then create nodes using new Node{value, nullptr, nullptr}. Finally, link children to root.
2
Add helper function to check BST
Add a function called isBST that takes a Node* called node, and two int parameters min and max. This function returns true if the subtree rooted at node is a BST with all values strictly between min and max. If node is nullptr, return true. Otherwise, check if node->data is within the range, and recursively check left and right subtrees with updated ranges.
DSA C++
Hint
Use recursion to check left subtree with max as current node's data, and right subtree with min as current node's data.
3
Use the helper function to check the tree
In main, call isBST with root, INT_MIN, and INT_MAX as arguments. Store the result in a boolean variable called result.
DSA C++
Hint
Call isBST with root, INT_MIN, and INT_MAX. Store the result in result.
4
Print if the tree is BST or not
Add a print statement that outputs "BST" if result is true, otherwise outputs "Not BST".
DSA C++
Hint
Use cout to print "BST" if result is true, else print "Not BST".
Practice
(1/5)
1. What is the main property that defines a Binary Search Tree (BST)?
easy
A. Every node has exactly two children.
B. For every node, all nodes in the left subtree are smaller and all nodes in the right subtree are larger.
C. All leaf nodes are at the same depth.
D. The tree is perfectly balanced.
Solution
Step 1: Understand BST property
A BST requires that for each node, values in the left subtree are smaller and values in the right subtree are larger.
Step 2: Compare other options
Options B, C, and D describe other tree properties but not BST specifically.
Final Answer:
For every node, all nodes in the left subtree are smaller and all nodes in the right subtree are larger. -> Option B
Quick Check:
BST property = left < node < right [OK]
Hint: Remember BST means left < node < right for all nodes [OK]
Common Mistakes:
Confusing BST with balanced tree
Thinking all nodes must have two children
Assuming leaf nodes must be at same depth
2. Which of the following C++ function signatures is correct to check if a binary tree is a BST using min and max limits?
easy
A. bool isBST(Node* root, int min, int max);
B. bool isBST(Node root, int min, int max);
C. bool isBST(Node* root, int min);
D. bool isBST(Node root);
Solution
Step 1: Check parameter types
To check BST, we need a pointer to the root node and min/max limits to validate node values.
Step 2: Validate function signature
bool isBST(Node* root, int min, int max); uses Node* root and both min and max parameters, which is correct for BST validation.
Final Answer:
bool isBST(Node* root, int min, int max); -> Option A
Quick Check:
Use pointer and min/max for BST check [OK]
Hint: Use pointer and min/max parameters for BST validation [OK]
Common Mistakes:
Passing Node by value instead of pointer
Missing max parameter
Using incorrect parameter types
3. What will be the output of the following code snippet?
Root is 10, left child 5 < 10, right child 15 > 10, so BST property holds.
Step 2: Check BST validation function
Function correctly checks node values within min and max limits recursively.
Final Answer:
Yes -> Option D
Quick Check:
Tree satisfies BST rules = Yes [OK]
Hint: Check left < root < right for all nodes [OK]
Common Mistakes:
Confusing <= and < in comparisons
Not passing correct min/max values
Ignoring nullptr base case
4. Identify the bug in this BST validation code snippet:
bool isBSTUtil(Node* root, int min, int max) {
if (!root) return true;
if (root->data < min || root->data > max) return false;
return isBSTUtil(root->left, min, root->data) && isBSTUtil(root->right, root->data, max);
}
medium
A. The function should not use recursion.
B. The base case should return false when root is nullptr.
C. The comparison should use <= min and >= max to exclude equal values.
D. The min and max parameters should be swapped in recursive calls.
Solution
Step 1: Check comparison operators
BST requires strict inequality: node values must be greater than min and less than max, so use <= and >= to reject equal values.
Step 2: Validate base case and recursion
Base case returning true for nullptr is correct; recursion is necessary; min and max parameters are correctly passed.
Final Answer:
The comparison should use <= min and >= max to exclude equal values. -> Option C
Quick Check:
Use strict inequalities to validate BST [OK]
Hint: Use <= and >= to reject duplicates in BST check [OK]
Common Mistakes:
Allowing equal values breaks BST property
Returning false on nullptr causes errors
Swapping min and max in recursion
5. Given a binary tree where nodes can have duplicate values, how would you modify the BST validation function to allow duplicates only on the right subtree?
hard
A. Allow duplicates on right subtree by changing left subtree check to root->data < min and right subtree check to root->data >= max.
B. Change the left subtree check to root->data <= min and right subtree check to root->data > max.
C. Allow duplicates on left subtree by changing comparison to root->data <= min.
D. Disallow duplicates completely by using strict inequalities everywhere.
Solution
Step 1: Understand duplicate placement rule
Duplicates allowed only on right subtree means left subtree nodes must be strictly less than root.
Step 2: Adjust comparison operators
Left subtree check uses < min (strict), right subtree check uses >= max (allow equal or greater).
Final Answer:
Allow duplicates on right subtree by changing left subtree check to root->data < min and right subtree check to root->data >= max. -> Option A
Quick Check:
Duplicates only right = left < root, right >= root [OK]
Hint: Allow duplicates on right by using >= in right subtree check [OK]
Common Mistakes:
Allowing duplicates on left subtree incorrectly
Using <= on left subtree allowing duplicates there
Not adjusting comparisons for duplicates
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.