📖 Scenario: You are working with a simple binary tree structure in Java. Your task is to check if this tree follows the rules of a Binary Search Tree (BST). A BST is a tree where for every node, all nodes in its left subtree have smaller values, and all nodes in its right subtree have larger values.This is important because BSTs allow quick searching, just like looking up a word in a dictionary.
🎯 Goal: Build a Java program that creates a binary tree, sets up a helper method to check if the tree is a BST, and then prints the result.
📋 What You'll Learn
Create a simple binary tree with exact node values and structure
Add a helper method to check BST validity using min and max value limits
Use recursion to check each node's value against allowed min and max
Print true if the tree is a BST, otherwise false
💡 Why This Matters
🌍 Real World
BSTs are used in databases and search engines to quickly find data by keeping it sorted.
💼 Career
Understanding BST validation helps in roles involving data structures, algorithms, and software development where efficient data lookup is needed.
Progress0 / 4 steps
1
Create the Binary Tree
Create a class called Node with integer value, and left and right child nodes. Then create a binary tree with root node value 10, left child 5, and right child 15. The left child of 5 is 2 and right child is 7. The right child of 15 is 20.
DSA Java
Hint
Start by defining the Node class with a constructor. Then create the tree nodes exactly as described.
2
Add Helper Method Signature
Inside the Main class, add a static method called isBST that takes a Node called node, and two integers min and max as parameters. This method will help check if the tree is a BST.
DSA Java
Hint
Write the method header exactly as public static boolean isBST(Node node, int min, int max).
3
Implement BST Check Logic
Complete the isBST method to check if the tree is a BST. Return true if node is null. Return false if node.value is not between min and max. Recursively check the left subtree with updated max as node.value - 1 and the right subtree with updated min as node.value + 1. Return true only if both subtrees are BSTs.
DSA Java
Hint
Use recursion and check the node value against min and max limits. Return false if out of range, otherwise check left and right subtrees with updated limits.
4
Print the Result
In the main method, call isBST with root, Integer.MIN_VALUE, and Integer.MAX_VALUE. Store the result in a boolean variable called result. Then print result.
DSA Java
Hint
Call isBST with the root and full integer range, save the result, and print it.
Practice
(1/5)
1. What is the main property that defines a Binary Search Tree (BST)?
easy
A. For each node, all nodes in the left subtree are smaller and all nodes in the right subtree are larger
B. Each node has exactly two children
C. All nodes have unique values but no order is required
D. The tree is always balanced
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 A
Quick Check:
BST property = For each node, all nodes in the left subtree are smaller and all nodes in the right subtree are larger [OK]
Hint: Remember BST means left < node < right [OK]
Common Mistakes:
Confusing BST with balanced tree
Thinking all nodes must have two children
Ignoring the ordering property
2. Which of the following Java method signatures is correct to check if a binary tree is a BST?
easy
A. public int isBST(Node root, int min, int max)
B. public boolean isBST(Node root, int min, int max)
C. public void isBST(Node root)
D. public boolean isBST(Node root, Node min, Node max)
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 B
Quick Check:
Return boolean with int min/max params [OK]
Hint: Use boolean return and int min/max for range checks [OK]
Common Mistakes:
Using void return type for a check method
Using Node type for min/max instead of int
Returning int instead of boolean
3. What will be the output of the following code snippet?
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));
}
}
medium
A. Runtime exception
B. true
C. Compilation error
D. false
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 D
Quick Check:
Node 12 violates BST rule, so false [OK]
Hint: Check if any left child > root or right child < root [OK]
Common Mistakes:
Ignoring subtree value ranges
Assuming all nodes in left subtree are less than root only
Not updating min/max during recursion
4. Identify the bug in this BST validation code snippet: