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
✗ Incorrect
The BST property requires all nodes in the left subtree to be smaller and all nodes in the right subtree to be larger than the current node.
Which method helps track allowed values during BST validation?
AUsing min and max value limits
BChecking only immediate children
CCounting nodes
DSorting the tree
✗ Incorrect
Min and max limits help ensure each node's value fits the BST property during recursive checks.
What is the time complexity of validating a BST with the min-max approach?
AO(log n)
BO(n)
CO(n^2)
DO(1)
✗ Incorrect
Each node is visited once, so the time complexity is linear, O(n).
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
✗ Incorrect
BST rules apply to all nodes in subtrees, so checking only immediate children misses violations deeper in the tree.
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
✗ Incorrect
Validation returns True if the tree satisfies BST properties, otherwise False.
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
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: