What if you could instantly know if your tree is perfectly sorted without checking every node by hand?
Why Validate if Tree is BST in DSA Java?
Imagine you have a family tree drawn on paper. You want to check if the tree follows a special rule: every child on the left is smaller than the parent, and every child on the right is bigger. Doing this by looking at each node and comparing manually is like checking every family member's age one by one without any order.
Manually checking each node against all others is slow and confusing. You might miss some nodes or compare the wrong ones. It's easy to make mistakes and waste time, especially if the tree is big.
Using a method to validate if the tree is a Binary Search Tree (BST) helps you quickly check if the tree follows the rules. It uses a smart way to compare nodes by keeping track of allowed value ranges as it moves through the tree, making the check fast and reliable.
boolean isBST(Node root) {
// Check each node against all others manually
// Very complex and slow
return false;
}boolean isBST(Node node, int min, int max) {
if (node == null) 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 lets you quickly and correctly confirm if a tree is a BST, enabling efficient searching and sorting operations.
When building a phone contact list app, ensuring the contact names are stored in a BST allows fast searching and adding new contacts without errors.
Manual checking is slow and error-prone.
Validating BST uses value limits to check nodes efficiently.
This method ensures fast and correct tree validation.
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
