A Binary Search Tree is a tree where each node has at most two children. 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 property to check when validating if a tree is a BST?
We must ensure that for every node, all values in the left subtree are less than the node's value, and all values in the right subtree are greater than the node's value.
Click to reveal answer
intermediate
Why can't we just check if left child < node < right child to validate BST?
Because the BST property applies to the entire left and right subtrees, not just immediate children. A violation can happen deeper in the subtree.
Click to reveal answer
intermediate
What approach can we use to validate a BST efficiently?
Use recursion with a range of allowed values for each node. Initially, the range is infinite. For left child, update the upper bound; for right child, update the lower bound.
Click to reveal answer
beginner
What is the time complexity of validating a BST using the range method?
The time complexity is O(n), where n is the number of nodes, because each node is visited once.
Click to reveal answer
Which condition must hold true for every node in a BST?
ANode value is equal to its children
BLeft child < node and right child > node only
CAll nodes in left subtree < node < all nodes in right subtree
DNo condition is needed
✗ Incorrect
The BST property requires all nodes in the left subtree to be less than the node, and all nodes in the right subtree to be greater.
What is the best way to validate a BST?
AUse recursion with min and max value limits
BCheck immediate children only
CTraverse the tree randomly
DCount the number of nodes
✗ Incorrect
Using recursion with min and max limits ensures all nodes satisfy BST properties throughout the tree.
What is the time complexity of validating a BST with n nodes?
AO(n)
BO(log n)
CO(1)
DO(n^2)
✗ Incorrect
Each node is visited once, so the time complexity is O(n).
If a node violates the BST property, what should the validation function do?
AContinue checking other nodes
BReturn false immediately
CIgnore the violation
DDelete the node
✗ Incorrect
The function should return false immediately when a violation is found.
Which traversal can help check if a tree is a BST by producing a sorted sequence?
APreorder traversal
BLevel order traversal
CPostorder traversal
DInorder traversal
✗ Incorrect
Inorder traversal of a BST produces nodes in ascending order.
Explain how to validate if a binary tree is a BST using recursion and value ranges.
Think about limiting the allowed values for each subtree.
You got /5 concepts.
Describe why checking only immediate children is not enough to validate a BST.
Consider a node deep in the left subtree that violates the BST property.
You got /4 concepts.
Practice
(1/5)
1. What does it mean for a binary tree to be a Binary Search Tree (BST)?
easy
A. All nodes have the same value.
B. Every node has exactly two children.
C. The tree is balanced with equal height on both sides.
D. Every node's left subtree contains values less than the node, and right subtree contains values greater.
Solution
Step 1: Understand BST property
A BST requires that for each node, all values in the left subtree are less than the node's value, and all values in the right subtree are greater.
Step 2: Compare options with definition
Every node's left subtree contains values less than the node, and right subtree contains values greater. matches the BST property exactly, while others describe different tree properties.
Final Answer:
Every node's left subtree contains values less than the node, and right subtree contains values greater. -> Option D
Quick Check:
BST property = Every node's left subtree contains values less than the node, and right subtree contains values greater. [OK]
Hint: BST means left < node < right for every node [OK]
Common Mistakes:
Confusing BST with balanced tree
Thinking all nodes must have two children
Assuming all values are equal
2. Which Go function signature correctly represents a function to check if a binary tree is a BST?
easy
A. func isBST(root *Node) bool
B. func isBST(root Node) int
C. func checkBST(root *Node) string
D. func validateBST(root Node) void
Solution
Step 1: Identify correct parameter and return types
The function should accept a pointer to the root node (to avoid copying) and return a boolean indicating if the tree is BST.
Step 2: Match with options
func isBST(root *Node) bool matches this: it takes *Node and returns bool. Others have wrong parameter types or return types.
B. It only compares immediate children, missing deeper subtree violations.
C. It uses wrong comparison operators for BST.
D. It causes infinite recursion.
Solution
Step 1: Analyze comparison logic
The function compares only node.Left.Val and node.Right.Val with node.Val, ignoring entire subtree values.
Step 2: Understand BST violation cases
A violation can occur deeper in subtrees, e.g., a left subtree node greater than root, which this function misses.
Final Answer:
It only compares immediate children, missing deeper subtree violations. -> Option B
Quick Check:
Check all subtree nodes, not just immediate children [OK]
Hint: Check entire subtree ranges, not just direct children [OK]
Common Mistakes:
Checking only immediate children values
Not passing min/max bounds in recursion
Assuming local checks guarantee global BST
5. You want to validate if a binary tree is a BST but the tree nodes can have duplicate values. Which modification to the standard BST validation logic is correct to allow duplicates only on the right subtree?
hard
A. Change the condition to allow node.Val <= max for left subtree and node.Val > max for right subtree.
B. Allow duplicates only if node.Val >= min for left subtree and node.Val < max for right subtree.
C. Allow duplicates only if node.Val > min for left subtree and node.Val <= max for right subtree.
D. Allow duplicates on both sides without changing conditions.
Solution
Step 1: Understand duplicate placement in BST
Duplicates allowed only on right means left subtree nodes must be strictly less than node.Val, right subtree nodes can be equal or greater.
Step 2: Adjust min/max conditions accordingly
Change violation check to (node.Val <= min || node.Val > max), enforcing node.Val > min && node.Val <= max. This allows equality to max (parent.Val in right calls) but enforces > min (parent.Val in left calls).
Final Answer:
Allow duplicates only if node.Val > min for left subtree and node.Val <= max for right subtree. -> Option C
Quick Check:
> min && <= max = duplicates on right only [OK]
Hint: Use > min && <= max to allow duplicates on right [OK]
Common Mistakes:
Allowing duplicates on left subtree
Not adjusting inequality signs properly
Allowing duplicates on both sides without rules
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.