Bird
Raised Fist0
DSA Javaprogramming~5 mins

Validate if Tree is BST in DSA Java - Time & Space Complexity

Choose your learning style10 modes available
Time Complexity: Validate if Tree is BST
O(n)
Understanding Time 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?

Scenario Under Consideration

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.

Identify Repeating Operations
  • Primary operation: Recursive calls visiting each node once.
  • How many times: Once per node in the tree.
How Execution Grows With Input

Each node is checked once, so the time grows directly with the number of nodes.

Input Size (n)Approx. Operations
10About 10 checks
100About 100 checks
1000About 1000 checks

Pattern observation: Time grows in a straight line with the number of nodes.

Final Time Complexity

Time Complexity: O(n)

This means the time to check grows directly with the number of nodes in the tree.

Common Mistake

[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.

Interview Connect

Understanding this helps you show how to carefully check tree properties using recursion, a key skill in many coding challenges.

Self-Check

"What if we used an in-order traversal to check the BST property instead? How would the time complexity change?"

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