Validate if Tree is BST in DSA Javascript - 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 when the tree gets bigger?
Analyze the time complexity of the following code snippet.
function isBST(node, min = -Infinity, max = Infinity) {
if (!node) return true;
if (node.value <= min || node.value >= max) return false;
return isBST(node.left, min, node.value) && isBST(node.right, node.value, max);
}
This code checks if a binary tree is a BST by making sure every node's value fits between allowed min and max limits.
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 number of nodes grows, the function visits each node one time.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks |
| 100 | About 100 checks |
| 1000 | About 1000 checks |
Pattern observation: The time grows directly with the number of nodes.
Time Complexity: O(n)
This means the time to check grows in a straight line with the number of nodes in the tree.
[X] Wrong: "Checking only the immediate children is enough to confirm a BST."
[OK] Correct: Because a node deeper in the tree might break the BST rule, so we must check all nodes with proper min and max limits.
Understanding this helps you show how to carefully check all parts of a tree, a skill useful in many tree problems.
"What if we remove the min and max limits and only compare a node with its immediate children? How would the time complexity and correctness change?"
Practice
Solution
Step 1: Understand BST property
A BST requires that for each node, all nodes in the left subtree have smaller values and all nodes in the right subtree have larger values.Step 2: Compare options with definition
Every node's left child is smaller and right child is larger than the node correctly states this property. Other options describe different tree properties.Final Answer:
Every node's left child is smaller and right child is larger than the node -> Option BQuick Check:
BST property = Every node's left child is smaller and right child is larger than the node [OK]
- Confusing BST with balanced tree
- Thinking all nodes must have two children
- Assuming all values are equal
Solution
Step 1: Identify common BST validation parameters
To validate BST, we usually pass the current node and allowed min and max values to check constraints.Step 2: Match function signatures
function isBST(node, min, max) {} matches this pattern with node, min, and max parameters. Others have extra or missing parameters.Final Answer:
function isBST(node, min, max) {} -> Option AQuick Check:
Correct BST function signature = function isBST(node, min, max) {} [OK]
- Adding unnecessary parameters
- Missing min or max limits
- Using wrong parameter names
class Node {
constructor(val, left = null, right = null) {
this.val = val;
this.left = left;
this.right = right;
}
}
function isBST(node, min = -Infinity, max = Infinity) {
if (!node) return true;
if (node.val <= min || node.val >= max) return false;
return isBST(node.left, min, node.val) && isBST(node.right, node.val, max);
}
const root = new Node(10, new Node(5), new Node(15, new Node(6), new Node(20)));
console.log(isBST(root));Solution
Step 1: Analyze tree structure
Root is 10, left child 5 (valid), right child 15 with left child 6 and right child 20.Step 2: Check BST property on right subtree
Node 6 is left child of 15 but 6 < 10, which violates BST property because all right subtree nodes must be > 10.Final Answer:
false -> Option CQuick Check:
6 in right subtree < 10 breaks BST = false [OK]
- Ignoring subtree value constraints
- Assuming all children just compare with parent
- Missing base case for null nodes
function isBST(node) {
if (!node) return true;
if (node.left && node.left.val >= node.val) return false;
if (node.right && node.right.val <= node.val) return false;
return isBST(node.left) && isBST(node.right);
}Solution
Step 1: Understand current checks
Function only compares immediate children with current node, missing subtree constraints.Step 2: Identify fix
To correctly validate BST, function must track allowed min and max values for each subtree, not just immediate children.Final Answer:
Add min and max parameters to track value ranges -> Option AQuick Check:
Use min/max limits to validate BST correctly [OK]
- Only checking immediate children
- Ignoring subtree value constraints
- Not using recursion properly
node.val <= min and node.val >= max checks in the recursive function.Solution
Step 1: Understand duplicates in BST
Allowing duplicates only on right means left subtree nodes must be strictly less than current node, right subtree nodes can be greater or equal.Step 2: Adjust min/max checks accordingly
Left subtree check should benode.val < min(strictly less), right subtree check should benode.val >= max(allow equal).Final Answer:
Allow duplicates on right by changing left subtree check to node.val < min and right subtree check to node.val >= max -> Option DQuick Check:
Duplicates only right: left < node, right >= node [OK]
- Allowing duplicates on both sides
- Using non-strict inequalities on left subtree
- Confusing min and max checks
Ready to prove it?
Pass the test and this pattern moves unknown
