In a BST, for every node, all values in its left subtree are smaller, and all values in its right subtree are larger than the node's value.
Click to reveal answer
beginner
Why does the BST property help in searching for a value?
Because the tree is ordered, we can decide to go left or right at each node, cutting the search space in half each time, making search fast.
Click to reveal answer
intermediate
What happens if the BST property is not maintained?
The tree loses its order, so searching becomes like checking every node, which is slow and inefficient.
Click to reveal answer
beginner
How does the BST property affect insertion of new nodes?
New nodes are placed so that the BST property stays true, by comparing values and going left or right until the right spot is found.
Click to reveal answer
intermediate
Explain how the BST property helps in in-order traversal.
In-order traversal visits nodes in ascending order because it first visits left subtree (smaller values), then node, then right subtree (larger values).
Click to reveal answer
What must be true for every node in a BST?
AAll subtree values are equal to node
BLeft subtree values > node > right subtree values
CLeft subtree values < node < right subtree values
DNo order is required
✗ Incorrect
The BST property requires left subtree values to be smaller and right subtree values to be larger than the node.
Why is searching faster in a BST compared to an unordered tree?
ABecause BST property allows skipping half the tree at each step
BBecause BST has fewer nodes
CBecause BST stores values in a list
DBecause BST uses hashing
✗ Incorrect
The BST property lets us decide to go left or right, reducing search space quickly.
What happens if you insert a value smaller than the current node in a BST?
AGo to the left subtree
BIgnore the value
CReplace the current node
DGo to the right subtree
✗ Incorrect
Smaller values go to the left subtree to maintain BST property.
Which traversal of a BST gives values in ascending order?
APre-order
BPost-order
CLevel-order
DIn-order
✗ Incorrect
In-order traversal visits left subtree, node, then right subtree, producing sorted order.
If BST property is broken, what is the impact on search?
ASearch becomes faster
BSearch becomes slower, like linear search
CSearch is unaffected
DSearch stops working
✗ Incorrect
Without BST property, we cannot skip parts of the tree, making search slower.
Describe the BST property and explain why it is important for efficient searching.
Think about how the tree is ordered and how that helps find values quickly.
You got /5 concepts.
Explain how the BST property affects insertion and traversal of nodes.
Consider how new values find their place and how visiting nodes in order works.
You got /4 concepts.
Practice
(1/5)
1. What is the main property that defines a Binary Search Tree (BST)?
easy
A. For each node, all values in the left subtree are smaller, and all values in the right subtree are larger.
B. Each node has exactly two children.
C. All nodes have the same value.
D. Nodes are arranged in a complete binary tree shape.
Solution
Step 1: Understand BST definition
A BST is defined by the order of values: left subtree nodes are smaller, right subtree nodes are larger than the current node.
Step 2: Compare options with BST property
For each node, all values in the left subtree are smaller, and all values in the right subtree are larger. matches the BST property exactly. Other options describe different tree types or incorrect properties.
Final Answer:
For each node, all values in the left subtree are smaller, and all values in the right subtree are larger. -> Option A
Quick Check:
BST property = For each node, all values in the left subtree are smaller, and all values in the right subtree are larger. [OK]
Hint: Remember: left < node < right in BST [OK]
Common Mistakes:
Thinking BST requires exactly two children per node
Confusing BST with balanced or complete trees
Assuming all nodes have the same value
2. Which of the following Java method signatures correctly checks if a tree node satisfies the BST property for its immediate children?
3. Consider the following Java code snippet that inserts values into a BST. What will be the in-order traversal output after inserting 10, 5, 15, 3, 7?
medium
A. 10 -> 5 -> 3 -> 7 -> 15 -> null
B. 15 -> 10 -> 7 -> 5 -> 3 -> null
C. 3 -> 5 -> 7 -> 10 -> 15 -> null
D. 5 -> 3 -> 7 -> 10 -> 15 -> null
Solution
Step 1: Insert nodes into BST
Insert 10 as root, 5 to left, 15 to right, 3 left of 5, 7 right of 5.
Step 2: Perform in-order traversal
In-order visits left subtree, node, right subtree: 3, 5, 7, 10, 15.
Final Answer:
3 -> 5 -> 7 -> 10 -> 15 -> null -> Option C
Quick Check:
In-order traversal of BST = sorted values [OK]
Hint: In-order traversal of BST gives sorted order [OK]
4. Given this Java method to check if a tree is a BST, what is the bug?
boolean isBST(TreeNode node, int min, int max) {
if (node == null) return true;
if (node.val <= min || node.val >= max) return false;
return isBST(node.left, min, node.val) && isBST(node.right, node.val, max);
}
medium
A. The method should use < and > instead of <= and >= for comparisons.
B. The base case should return false when node is null.
C. The method does not check the entire subtree values properly.
D. The initial call must provide correct min and max values.
Solution
Step 1: Understand method logic
The method uses min and max bounds to check BST property recursively.
Step 2: Identify bug
The method logic is correct, but if initial min and max are not set properly (e.g., Integer.MIN_VALUE and Integer.MAX_VALUE), it may fail.
Final Answer:
The initial call must provide correct min and max values. -> Option D
Quick Check:
Proper initial bounds needed = The initial call must provide correct min and max values. [OK]
Hint: Always start with full integer range for min and max [OK]
Common Mistakes:
Changing base case to false on null node
Misusing <= and >= causing off-by-one errors
Ignoring initial min/max parameters in first call
5. You have a BST where duplicate values are allowed only on the right subtree. Which modification to the BST property is correct to maintain this rule?
hard
A. Left subtree nodes <= node.val, right subtree nodes > node.val
B. Left subtree nodes < node.val, right subtree nodes >= node.val
C. Left subtree nodes <= node.val, right subtree nodes >= node.val
D. Left subtree nodes < node.val, right subtree nodes > node.val
Solution
Step 1: Understand duplicate placement rule
Duplicates allowed only on right subtree means right subtree can have values equal or greater than node.
Step 2: Adjust BST property accordingly
Left subtree must have strictly smaller values, right subtree must have values greater or equal to node.
Final Answer:
Left subtree nodes < node.val, right subtree nodes >= node.val -> Option B
Quick Check:
Duplicates on right = left < node, right >= node [OK]
Hint: Allow equals only on right side with >= [OK]
Common Mistakes:
Allowing duplicates on left subtree
Using <= on left subtree allowing duplicates there
Forgetting strict less than on left subtree
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.