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 idea to check if a binary tree is a BST?
The main idea is to verify that every node's value lies within a valid range, which is updated as we traverse the tree, ensuring left children are less and right children are greater than the current node.
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 this only checks immediate children. The BST property must hold for all descendants, so we must check the entire subtree values against valid ranges, not just direct children.
Click to reveal answer
beginner
What is the time complexity of validating a BST using range limits?
The time complexity is O(n), where n is the number of nodes, because we visit each node once to check its value against the valid range.
Click to reveal answer
beginner
Show a simple C# method signature to validate if a tree is BST.
public bool IsValidBST(TreeNode root) { /* implementation */ }
Click to reveal answer
What must be true for every node in a BST?
ALeft subtree nodes are smaller, right subtree nodes are larger
BLeft subtree nodes are larger, right subtree nodes are smaller
CAll nodes have equal values
DNo children nodes
✗ Incorrect
In a BST, all nodes in the left subtree must be smaller and all nodes in the right subtree must be larger than the current node.
Which approach correctly validates a BST?
ACheck only immediate children values
BCount nodes in left and right subtree
CCheck if tree is balanced
DCheck node values against a valid range during traversal
✗ Incorrect
Validating BST requires checking each node's value against a valid range that updates as we traverse the tree.
What is the time complexity of validating a BST by checking all nodes once?
AO(log n)
BO(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?
AReturn false immediately
BReturn true
CIgnore and continue
DDelete the node
✗ Incorrect
If any node violates the BST property, the tree is not a BST, so the function should return false immediately.
Which data structure is commonly used to represent nodes in a BST in C#?
AList
BArray
CTreeNode class with left and right children
DDictionary
✗ Incorrect
A TreeNode class with left and right child references is commonly used to represent BST nodes.
Explain how to validate if a binary tree is a BST using range limits.
Think about how the valid range changes as you move down the tree.
You got /5 concepts.
Describe why checking only immediate children is not enough to validate a BST.
Consider a node's left child is valid but a grandchild violates the BST property.
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 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 matches the BST property exactly, while others describe different tree properties.
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 C# method signatures is correct to check if a binary tree is a BST?
easy
A. bool IsBST(Node root, int min, int max)
B. void IsBST(Node root)
C. int IsBST(Node root)
D. bool IsBST(Node root, Node parent)
Solution
Step 1: Identify parameters needed for BST validation
To validate BST, we need to check node values within a range (min, max) as we traverse.
Step 2: Match method signature with required parameters
bool IsBST(Node root, int min, int max) includes root node and min/max range, suitable for recursive BST check.
Final Answer:
bool IsBST(Node root, int min, int max) -> Option A
Quick Check:
Method with min/max range = bool IsBST(Node root, int min, int max) [OK]
Hint: Use min and max range parameters for BST validation [OK]
Common Mistakes:
Using void return type instead of bool
Missing min and max parameters for range checks
Passing parent node instead of value ranges
3. What will be the output of the following C# code snippet?
class Node {
public int val;
public Node left, right;
public Node(int v) { val = v; left = right = null; }
}
bool IsBST(Node 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);
}
var 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);
Console.WriteLine(IsBST(root, int.MinValue, int.MaxValue));
medium
A. True
B. False
C. Compilation error
D. Runtime exception
Solution
Step 1: Trace the tree structure and BST property
The tree has root 10, left child 5, right child 15. Left child's right child is 12, which is greater than 10 but in left subtree.
Step 2: Check BST validation logic
IsBST will fail because 12 is in left subtree of 10 but greater than 10, violating BST rules.
Final Answer:
False -> Option B
Quick Check:
Node 12 in left subtree breaks BST = False [OK]
Hint: Check if left subtree nodes are less than root [OK]
Common Mistakes:
Assuming all nodes are valid without checking subtree ranges
Ignoring node 12's position violating BST
Confusing left and right subtree values
4. Identify the bug in the following C# code that checks if a binary tree is a BST: