A Binary Search Tree is a tree where each node has at most two children. For every node, all nodes in its left subtree have smaller values, and all nodes in its right subtree have larger values.
Click to reveal answer
beginner
What is the main property to check when validating if a tree is a BST?
We must ensure that for every node, all values in the left subtree are less than the node's value, and all values in the right subtree are greater than the node's value.
Click to reveal answer
intermediate
Why can't we just check if the left child is less and right child is greater to validate a BST?
Because the BST property applies to all descendants, not just immediate children. A node deep in the left subtree must be less than the root, not just its parent.
Click to reveal answer
intermediate
What is a common approach to validate a BST using recursion?
Use a helper function that passes down the allowed minimum and maximum values for each node. Check if the node's value lies within these limits, then recursively check left and right subtrees with updated limits.
Click to reveal answer
beginner
What is the time complexity of validating a BST using the min/max recursion method?
The time complexity is O(n), where n is the number of nodes, because each node is visited once.
Click to reveal answer
Which condition must hold true for every node in a BST?
ALeft subtree nodes < node < Right subtree nodes
BLeft subtree nodes > node > Right subtree nodes
CLeft child < node and Right child > node only
DAll nodes have two children
✗ Incorrect
In a BST, all nodes in the left subtree must be less than the node, and all nodes in the right subtree must be greater.
What is the best way to validate a BST?
ACheck only immediate children values
BUse inorder traversal and check if sorted
CCheck if root is greater than left child
DCount nodes in the tree
✗ Incorrect
Inorder traversal of a BST produces sorted values. Checking if the inorder traversal is sorted validates the BST.
What does the min/max recursion method do when validating a BST?
ASwaps nodes to fix the tree
BCounts nodes in left and right subtree
CChecks only leaf nodes
DPasses allowed value range down the tree
✗ Incorrect
The method passes minimum and maximum allowed values to each node to ensure BST property holds.
What is the time complexity of validating a BST using inorder traversal?
AO(n)
BO(log n)
CO(n log n)
DO(n^2)
✗ Incorrect
Inorder traversal visits each node once, so the time complexity is O(n).
If a node violates the BST property, what should the validation function return?
ATrue
BNull
CFalse
DThe node value
✗ Incorrect
If any node violates the BST property, the tree is not a BST, so the function returns False.
Explain how to validate if a binary tree is a BST using recursion with min and max limits.
Think about how allowed value ranges change as you go down the tree.
You got /5 concepts.
Describe why checking only immediate children is not enough to validate a BST.
Consider a node far down the left subtree with a value greater than root.
You got /4 concepts.
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.