Bird
Raised Fist0
DSA Javaprogramming~5 mins

Validate if Tree is BST in DSA Java - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What does BST stand for and what is its key property?
BST stands for Binary Search Tree. Its key property is that for every node, all nodes in its left subtree have smaller values, and all nodes in its right subtree have larger values.
Click to reveal answer
beginner
What is the main approach to validate if a binary tree is a BST?
The main approach is to check if the tree satisfies the BST property at every node by ensuring all nodes in the left subtree are less than the node and all nodes in the right subtree are greater, often using min and max value limits during traversal.
Click to reveal answer
intermediate
Why can't we just check if the left child is less and right child is greater to validate BST?
Because the BST property applies to all descendants, not just immediate children. A node deep in the left subtree must be less than the root, so checking only direct children is not enough.
Click to reveal answer
intermediate
What role do min and max values play in BST validation?
Min and max values keep track of the allowed range for node values during recursion. Each node must be within these limits to satisfy BST rules.
Click to reveal answer
beginner
What is the time complexity of validating a BST using the min-max approach?
The time complexity is O(n), where n is the number of nodes, because each node is visited once during the validation.
Click to reveal answer
What must be true for every node in a BST?
ANode values are in ascending order from left to right
BLeft child is always smaller than right child
CAll nodes have two children
DAll nodes in the left subtree are smaller and all nodes in the right subtree are larger
Which method helps track allowed values during BST validation?
AUsing min and max value limits
BChecking only immediate children
CCounting nodes
DSorting the tree
What is the time complexity of validating a BST with the min-max approach?
AO(log n)
BO(n)
CO(n^2)
DO(1)
Why is checking only left and right child values not enough to validate a BST?
ABecause trees can be unbalanced
BBecause children can have equal values
CBecause BST property applies to all descendants, not just children
DBecause nodes can have more than two children
What does a valid BST output look like after validation?
ATrue if valid, False if not
BSorted list of node values
CHeight of the tree
DNumber of nodes
Explain how to validate if a binary tree is a BST using min and max values.
Think about how allowed value ranges change as you go down the tree.
You got /4 concepts.
    Describe why checking only immediate children is insufficient to validate a BST.
    Consider a node deep in the left subtree that violates BST rules.
    You got /3 concepts.

      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