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 the left subtree have smaller values, and all nodes in the right subtree have larger values.
Click to reveal answer
beginner
What is the main idea behind validating if a binary tree is a BST?
The main idea is to check if every node's value lies within a valid range defined by its ancestors, ensuring left children are smaller and right children are larger.
Click to reveal answer
intermediate
In TypeScript, what type of traversal is commonly used to validate a BST?
In-order traversal is commonly used because it visits nodes in ascending order if the tree is a BST.
Click to reveal answer
intermediate
Why can't we just check if left child < node < right child to validate BST?
Because this only checks immediate children, not the entire subtree. A node deeper in the left subtree might violate the BST property if it is greater than the root.
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 traversal order helps verify if a binary tree is a BST by checking ascending order?
APost-order traversal
BPre-order traversal
CLevel-order traversal
DIn-order traversal
✗ Incorrect
In-order traversal visits nodes in ascending order if the tree is a BST.
What must be true for every node in a BST regarding its left subtree?
AAll nodes in left subtree are smaller
BAll nodes in left subtree are larger
CLeft subtree can have any values
DLeft subtree must be empty
✗ Incorrect
In a BST, all nodes in the left subtree must have smaller values than the node.
Why is checking only immediate children insufficient to validate a BST?
ABecause only root matters
BBecause deeper nodes might violate BST property
CBecause BST allows duplicates anywhere
DBecause children can have equal values
✗ Incorrect
Nodes deeper in the subtree might violate the BST property even if immediate children are correct.
What is the best way to keep track of valid value ranges when validating BST?
ACheck only node values without range
BUse a queue to store nodes
CPass min and max allowed values down the recursion
DCompare node with root only
✗ Incorrect
Passing min and max allowed values ensures each node fits the BST property relative to ancestors.
What is the time complexity of validating a BST by visiting each node once?
AO(n)
BO(log n)
CO(n^2)
DO(1)
✗ Incorrect
Each node is visited once, so the time complexity is linear, O(n).
Explain how to validate if a binary tree is a BST using recursion and value ranges.
Think about how ancestors restrict node values.
You got /4 concepts.
Describe why in-order traversal output should be sorted for a BST and how this helps validation.
Consider the order nodes are visited in a BST.
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. Every node has exactly two children
B. The tree is balanced with equal height on both sides
C. All nodes have the same value
D. Every node's left subtree has smaller values and right subtree has larger values
Solution
Step 1: Understand BST property
A BST requires that for each node, all values in the left subtree are smaller and all values in the right subtree are larger.
Step 2: Compare options with definition
Every node's left subtree has smaller values and right subtree has larger values matches the BST property exactly, while others describe different tree properties.
Final Answer:
Every node's left subtree has smaller values and right subtree has larger values -> Option D
Quick Check:
BST property = Every node's left subtree has smaller values and right subtree has larger values [OK]
Hint: Remember BST means left < node < right for all nodes [OK]
Common Mistakes:
Confusing BST with balanced tree
Thinking all nodes must have two children
Assuming all values are equal
2. Which TypeScript function signature correctly represents a function to check if a binary tree is a BST?
easy
A. function isBST(root: any): void
B. function isBST(root: TreeNode | null): boolean
C. function isBST(root: TreeNode): number
D. function isBST(root: number): boolean
Solution
Step 1: Identify correct parameter and return types
The function should accept a tree node or null and return a boolean indicating if it is a BST.
Step 2: Match options with correct signature
function isBST(root: TreeNode | null): boolean matches this: input is TreeNode or null, output is boolean. Others have wrong types or return types.
Final Answer:
function isBST(root: TreeNode | null): boolean -> Option B
Quick Check:
Correct signature = function isBST(root: TreeNode | null): boolean [OK]
Hint: Input is root node or null, output is true/false [OK]
Common Mistakes:
Using wrong parameter type like number
Returning number instead of boolean
Using void return type
3. What will be the output of the following TypeScript code snippet?
class TreeNode {
val: number;
left: TreeNode | null;
right: TreeNode | null;
constructor(val: number, left: TreeNode | null = null, right: TreeNode | null = null) {
this.val = val;
this.left = left;
this.right = right;
}
}
function isBST(root: TreeNode | null, min: number = -Infinity, max: number = Infinity): boolean {
if (!root) return true;
if (root.val <= min || root.val >= max) return false;
return isBST(root.left, min, root.val) && isBST(root.right, root.val, max);
}
const tree = new TreeNode(10, new TreeNode(5), new TreeNode(15, new TreeNode(6), new TreeNode(20)));
console.log(isBST(tree));
medium
A. SyntaxError
B. true
C. false
D. RuntimeError
Solution
Step 1: Analyze the tree structure
The root is 10, left child 5 (valid), right child 15 with left child 6 and right child 20.
Step 2: Check BST property on right subtree
Node 6 is in right subtree of 10 but less than 10, violating BST property. So function returns false.
Final Answer:
false -> Option C
Quick Check:
6 in right subtree < 10 = false [OK]
Hint: Check if left < node < right recursively [OK]
Common Mistakes:
Ignoring min/max constraints in recursion
Assuming all subtrees are BST without checks
Confusing node values in subtrees
4. Identify the bug in this TypeScript function to check if a tree is BST:
function isBST(root: TreeNode | null): boolean {
if (!root) return true;
if (root.left && root.left.val > root.val) return false;
if (root.right && root.right.val < root.val) return false;
return isBST(root.left) && isBST(root.right);
}
medium
A. It does not check the entire subtree values against min/max limits
B. It uses wrong comparison operators for left and right children
C. It returns true for null nodes incorrectly
D. It does not handle empty tree input
Solution
Step 1: Understand the function checks
The function only compares immediate children values with current node, not entire subtree ranges.
Step 2: Explain why this is a bug
BST requires all nodes in left subtree < current node and all in right subtree > current node, not just direct children.
Final Answer:
It does not check the entire subtree values against min/max limits -> Option A
Quick Check:
Missing min/max range check = It does not check the entire subtree values against min/max limits [OK]
Hint: Check full subtree ranges, not just immediate children [OK]
Common Mistakes:
Only comparing direct children values
Ignoring recursive min/max constraints
Assuming null nodes cause errors
5. Given a binary tree where nodes can have duplicate values, which modification to the BST validation logic allows duplicates only on the right subtree?
Choose the correct condition to replace root.val <= min in the validation function:
function isBST(root: TreeNode | null, min: number = -Infinity, max: number = Infinity): boolean {
if (!root) return true;
if (root.val <= min || root.val >= max) return false;
return isBST(root.left, min, root.val) && isBST(root.right, root.val, max);
}
hard
A. Change to root.val < min to allow duplicates on right
B. Change to root.val <= min and root.val < max
C. Change to root.val < min and root.val <= max
D. Change to root.val <= min and root.val <= max
Solution
Step 1: Understand duplicates on right subtree rule
Duplicates allowed only on right means left subtree nodes must be strictly less than current node, right subtree nodes greater than or equal.
Step 2: Adjust min/max comparisons accordingly
Replace root.val <= min with root.val < min. This fails only if strictly less than min (allows equal to min on right recursion), keeping root.val >= max for strict upper bound.
Final Answer:
Change to root.val < min to allow duplicates on right -> Option A
Quick Check:
Duplicates right subtree = Change to root.val < min [OK]
Hint: Use strict less on left, less or equal on right for duplicates [OK]
Common Mistakes:
Allowing duplicates on left subtree
Using <= on both sides
Not changing min/max checks
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.