Validate if Tree is BST in DSA C++ - Time & Space Complexity
We want to know how long it takes to check if a tree follows the rules of a Binary Search Tree (BST).
How does the time needed grow as the tree gets bigger?
Analyze the time complexity of the following code snippet.
bool isBSTUtil(Node* node, int min, int max) {
if (node == nullptr) return true;
if (node->data <= min || node->data >= max) return false;
return isBSTUtil(node->left, min, node->data) &&
isBSTUtil(node->right, node->data, max);
}
bool isBST(Node* root) {
return isBSTUtil(root, INT_MIN, INT_MAX);
}
This code checks if a binary tree is a BST by making sure every node fits the BST rules within a range.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Recursive calls visiting each node once.
- How many times: Once per node in the tree.
As the tree grows, the function visits every node exactly once.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 visits |
| 100 | About 100 visits |
| 1000 | About 1000 visits |
Pattern observation: The time grows directly in proportion to the number of nodes.
Time Complexity: O(n)
This means the time to check the tree grows linearly with the number of nodes.
[X] Wrong: "Checking if a tree is BST takes more than linear time because of nested checks."
[OK] Correct: Each node is checked once with simple comparisons, so the total work is just one pass through all nodes.
Understanding this helps you explain how to efficiently verify tree properties, a common skill in coding interviews.
"What if the tree is given as a sorted array and you want to check if it can form a BST? How would the time complexity change?"
Practice
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 BQuick Check:
BST property = left < node < right [OK]
- Confusing BST with balanced tree
- Thinking all nodes must have two children
- Assuming leaf nodes must be at same depth
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 AQuick Check:
Use pointer and min/max for BST check [OK]
- Passing Node by value instead of pointer
- Missing max parameter
- Using incorrect parameter types
#include <iostream>
#include <climits>
struct Node {
int data;
Node* left;
Node* right;
};
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);
}
bool isBST(Node* root) {
return isBSTUtil(root, INT_MIN, INT_MAX);
}
int main() {
Node n1 = {10, nullptr, nullptr};
Node n2 = {5, nullptr, nullptr};
Node n3 = {15, nullptr, nullptr};
n1.left = &n2;
n1.right = &n3;
std::cout << (isBST(&n1) ? "Yes" : "No") << std::endl;
return 0;
}Solution
Step 1: Analyze tree structure
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 DQuick Check:
Tree satisfies BST rules = Yes [OK]
- Confusing <= and < in comparisons
- Not passing correct min/max values
- Ignoring nullptr base case
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);
}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 CQuick Check:
Use strict inequalities to validate BST [OK]
- Allowing equal values breaks BST property
- Returning false on nullptr causes errors
- Swapping min and max in recursion
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 AQuick Check:
Duplicates only right = left < root, right >= root [OK]
- Allowing duplicates on left subtree incorrectly
- Using <= on left subtree allowing duplicates there
- Not adjusting comparisons for duplicates
Ready to prove it?
Pass the test and this pattern moves unknown
