Bird
Raised Fist0
DSA Javaprogramming~30 mins

Validate if Tree is BST in DSA Java - Build from Scratch

Choose your learning style10 modes available
Validate if Tree is BST
📖 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

  1. 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.
  2. 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.
  3. Final Answer:

    For each node, all nodes in the left subtree are smaller and all nodes in the right subtree are larger -> Option A
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    public boolean isBST(Node root, int min, int max) -> Option B
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    false -> Option D
  4. 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:
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);
}
medium
A. It does not handle null root properly
B. It uses wrong comparison operators for left and right children
C. It does not check the entire subtree values, only immediate children
D. It should return int instead of boolean

Solution

  1. Step 1: Analyze the checks performed

    The code only compares root with its immediate left and right children, not the entire subtree.
  2. 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.
  3. Final Answer:

    It does not check the entire subtree values, only immediate children -> Option C
  4. Quick Check:

    Check full subtree ranges, not just children [OK]
Hint: Check full subtree ranges, not just direct children [OK]
Common Mistakes:
  • Only comparing immediate children values
  • Ignoring recursive min/max range checks
  • Assuming local checks are enough
5. Given a binary tree, which approach correctly validates if it is a BST considering edge cases like duplicate values and integer limits?
hard
A. Use min and max integer range parameters updated during recursion
B. Use in-order traversal to check if values are strictly increasing
C. Check only immediate children values recursively
D. Compare each node with root only, ignoring subtrees

Solution

  1. Step 1: Understand edge cases in BST validation

    Duplicates and integer limits require careful range checks, not just value comparisons.
  2. Step 2: Evaluate approaches

    Using min and max parameters updated during recursion correctly enforces BST property for all nodes and handles edge cases.
  3. Final Answer:

    Use min and max integer range parameters updated during recursion -> Option A
  4. Quick Check:

    Range checks with min/max handle edge cases [OK]
Hint: Use min/max range checks recursively for full validation [OK]
Common Mistakes:
  • Ignoring duplicates or equal values
  • Not updating min/max during recursion
  • Only checking immediate children
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard