Validate if Tree is BST in DSA Java - 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.
public boolean isBST(TreeNode root) {
return isBSTHelper(root, Long.MIN_VALUE, Long.MAX_VALUE);
}
private boolean isBSTHelper(TreeNode node, long min, long max) {
if (node == null) return true;
if (node.val <= min || node.val >= max) return false;
return isBSTHelper(node.left, min, node.val) && isBSTHelper(node.right, node.val, max);
}
This code checks if a binary tree is a BST by verifying each node's value is within allowed limits.
- Primary operation: Recursive calls visiting each node once.
- How many times: Once per node in the tree.
Each node is checked once, so the time grows directly with the number of nodes.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks |
| 100 | About 100 checks |
| 1000 | About 1000 checks |
Pattern observation: Time grows in a straight line with the number of nodes.
Time Complexity: O(n)
This means the time to check grows directly with the number of nodes in the tree.
[X] Wrong: "Checking only the immediate children is enough to confirm BST."
[OK] Correct: Because BST rules apply to all descendants, not just direct children, so we must check the whole subtree.
Understanding this helps you show how to carefully check tree properties using recursion, a key skill in many coding challenges.
"What if we used an in-order traversal to check the BST property instead? How would the time complexity change?"
Practice
Solution
Step 1: Understand BST property
A BST requires that for every node, values in the left subtree are smaller and values in the right subtree are larger.Step 2: Compare options with BST definition
For each node, all nodes in the left subtree are smaller and all nodes in the right subtree are larger correctly states this property, while others describe different tree characteristics.Final Answer:
For each node, all nodes in the left subtree are smaller and all nodes in the right subtree are larger -> Option AQuick Check:
BST property = For each node, all nodes in the left subtree are smaller and all nodes in the right subtree are larger [OK]
- Confusing BST with balanced tree
- Thinking all nodes must have two children
- Ignoring the ordering property
Solution
Step 1: Identify correct return type and parameters
Checking BST returns true/false, so return type should be boolean. Also, min and max values are needed as integers to validate node values.Step 2: Match method signature
public boolean isBST(Node root, int min, int max) matches this: boolean return type and int min, max parameters. Others have wrong return types or parameter types.Final Answer:
public boolean isBST(Node root, int min, int max) -> Option BQuick Check:
Return boolean with int min/max params [OK]
- Using void return type for a check method
- Using Node type for min/max instead of int
- Returning int instead of boolean
class Node {
int val;
Node left, right;
Node(int v) { val = v; left = right = null; }
}
public class Test {
static boolean isBST(Node root, int min, int max) {
if (root == null) return true;
if (root.val <= min || root.val >= max) return false;
return isBST(root.left, min, root.val) && isBST(root.right, root.val, max);
}
public static void main(String[] args) {
Node root = new Node(10);
root.left = new Node(5);
root.right = new Node(15);
root.left.left = new Node(1);
root.left.right = new Node(12);
System.out.println(isBST(root, Integer.MIN_VALUE, Integer.MAX_VALUE));
}
}Solution
Step 1: Trace the tree nodes and BST conditions
The node with value 12 is in the left subtree of 10 but greater than 10, violating BST property.Step 2: Check isBST calls
When checking root.left.right (12), it fails the condition root.val <= min or root.val >= max because 12 >= 10 is true, so returns false.Final Answer:
false -> Option DQuick Check:
Node 12 violates BST rule, so false [OK]
- Ignoring subtree value ranges
- Assuming all nodes in left subtree are less than root only
- Not updating min/max during recursion
boolean isBST(Node root) {
if (root == null) return true;
if (root.left != null && root.left.val > root.val) return false;
if (root.right != null && root.right.val < root.val) return false;
return isBST(root.left) && isBST(root.right);
}Solution
Step 1: Analyze the checks performed
The code only compares root with its immediate left and right children, not the entire subtree.Step 2: Understand BST validation requirement
BST validation requires all nodes in left subtree < root and all in right subtree > root, not just immediate children.Final Answer:
It does not check the entire subtree values, only immediate children -> Option CQuick Check:
Check full subtree ranges, not just children [OK]
- Only comparing immediate children values
- Ignoring recursive min/max range checks
- Assuming local checks are enough
Solution
Step 1: Understand edge cases in BST validation
Duplicates and integer limits require careful range checks, not just value comparisons.Step 2: Evaluate approaches
Using min and max parameters updated during recursion correctly enforces BST property for all nodes and handles edge cases.Final Answer:
Use min and max integer range parameters updated during recursion -> Option AQuick Check:
Range checks with min/max handle edge cases [OK]
- Ignoring duplicates or equal values
- Not updating min/max during recursion
- Only checking immediate children
Ready to prove it?
Pass the test and this pattern moves unknown
